I've looked around here for similiar problems, but for some reason my BroadcastReceiver never ends up receiving the android.intent.action.BOOT_COMPLETED Intent.
Here is my (relative) Android.Manifest File:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> <receiver android:name=".BootReceiver" android:enabled="true" android:exported="true" android:label="BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> </receiver>
And Here is the actual Receiver.
public class BootReceiver extends BroadcastReceiver { private static final String TAG="BootReceiver"; @Override public void onReceive(Context context,Intent intent){ try{ context.startService(new Intent(context,ConnectivityListener.class)); Log.i(TAG,"Starting Service ConnectivityListener"); }catch(Exception e){ Log.e(TAG,e.toString()); } } }
Thanks! Any help is greatly appreciated
As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app.
A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
You can emulate all broadcast actions by connecting via adb to the device and open a device shell.
Here we go:
adb shell
or on linux/mac ./adb shell
am broadcast -a android.intent.action.BOOT_COMPLETED
or whatever action you want to fireThere are a bunch of nice commands coming with adb or the adb shell. Just try it
Regards Flo
edit: oh damn, i wanted this answer as an answer on the "had to turn phone on/off every time". sorry folks
I'm posting this in the hope that it will be helpful to someone who has tried everything but still cannot get it to run on boot after installation or it used to work before and doesn't work anymore.
So assuming you have added the permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And registered your receiver:
<receiver android:name="com.example.startuptest.StartUpBootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
And coded your BroadcastReceiver
:
public class StartUpBootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED"); ... } } }
Starting with Android 3.1 all applications, upon installation, are placed in a "stopped" state.(This is the same state that the application ends up in after the user force-stops the app from the Settings application.)
While in "stopped" state, the application will not run for any reason, except by a manual launch of an activity. (Meaning no BroadcastRecevier
(ACTION_PACKAGE_INSTALLED
, BOOT_COMPLETED
etc. will be invoked, regardless of the event for which they have registered, until the user runs the app manually.)
This is a design decision by Google to prevent malware apps. Google has advocated that users should launch an activity from the launcher first, before that application can do much. Preventing BOOT_COMPLETED
from being delivered until the activity is launched is a logical consequence of that argument.
Once a user runs any activity in your app once, you will receive the BOOT_COMPLETED broadcast after all future boots.
More details about this:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/05/boot-completed-regression.html
http://devmaze.wordpress.com/2011/12/05/activating-applications/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With