Service is not running on BOOT COMPLETE

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

when it is running, a message appears with a toast.

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

however, if I try to start my service using an action, the service starts well and the toast disappears correctly after a few seconds.

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 out because I really don't need it, just need to check if the service starts from the activity.

+5
source share
3 answers

If your application has no actions, your BroadcastReceiver will never be called.

When installing the application, it is installed in a "stopped state". applications in a "stopped state" do not broadcast Intent .

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

Once your application is no longer in a stopped state, Android will send it an Intent message. That is, until the user "stops" your application.

If the user “forcibly stops” your application, it will return to the “stopped state” and will no longer receive the Intent s broadcast. Until the user starts your application again.

+3
source

I tried with am broadcast -a android.intent.action.BOOT_COMPLETED , then restarted the device.

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

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

0
source

Your service filters the actions, but your intentions do not provide. Fix it:

 StartServiceIntent.setAction("com.tfl.extprotocolservice.IExtMessage"); 
-1
source

All Articles