Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver not receiving BOOT_COMPLETED

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

like image 561
apmeyers1987 Avatar asked Feb 19 '11 15:02

apmeyers1987


People also ask

What is the limit of BroadcastReceiver in Android?

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.

What does receive boot broadcast mean?

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.


2 Answers

You can emulate all broadcast actions by connecting via adb to the device and open a device shell.

Here we go:

  • open console/terminal and navigating to /platform-tools
  • type adb shell or on linux/mac ./adb shell
  • in the shell type am broadcast -a android.intent.action.BOOT_COMPLETED or whatever action you want to fire

There 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

like image 53
fklappan Avatar answered Sep 24 '22 22:09

fklappan


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.)

Android stopped state

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/

like image 21
Caner Avatar answered Sep 25 '22 22:09

Caner