service not started on BOOT COMPLETE

2019-05-01 06:24发布

问题:

I have a service that I would like to start on BOOT COMPLETE

when it is started , I have a toast message displayed.

my problem is that when the device boots up , the toast is displayed and is stuck on screen, and the service is not starting correctly.

however if I am trying to start my service through an activity , the service is starting well and the toast disappears after a few seconds correctly.

my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tfl.extprotocolservice"
    android:versionCode="7"
    android:versionName="1.6" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.tfl.extprotocolservice.ExtProtocolBroadcastReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name=".ExtProtocolService" >
            <intent-filter>
                <action android:name="com.tfl.extprotocolservice.ISetIpPort" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.tfl.extprotocolservice.IExtMessage" />
            </intent-filter>
        </service>
 <!-- 
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 -->
    </application>

</manifest>

my broadcast receiver:

public class ExtProtocolBroadcastReceiver extends BroadcastReceiver {


    /* broadcast receiver to start on BOOT COMPLETE*/
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent StartServiceIntent=new Intent(context,ExtProtocolService.class);
        context.startService(StartServiceIntent);

    }

}

btw, the activity in the manifest is commented because I don't really need it , it was just to test starting the service from an activity.

回答1:

If your application has no activities, your BroadcastReceiver will never get called.

When you install an application, it is installed in the "stopped state". applications in "stopped state" do not get broadcast Intents delivered to them.

In order to get your application out of "stopped state", the user must manually launch your application (at least once). In order to do this, you must offer him an Activity that he can use to start your application.

Once your application is no longer in "stopped state", Android will deliver broadcast Intents to it. That is, until the user "force stops" your application.

If the user "force stops" your application, it will go back to "stopped state" and will no longer get the broadcast Intents. Until the user manually starts your application again.



回答2:

I tried with am broadcast -a android.intent.action.BOOT_COMPLETED then it restart the device.

You can try <action android:name="android.intent.action.USER_PRESENT"/>

After more research, I think it was the fastboot mode which will not broadcast BOOT_COMPLETE.



回答3:

Your service is filtering actions, but your intent doesn't provide any. Fix with this:

StartServiceIntent.setAction("com.tfl.extprotocolservice.IExtMessage");