Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast Receiver not getting the broadcast if app is not started

I have some 2 apps all installed on a device. One of the apps catch the ACTION_BOOT_COMPLETED broadcast, does some initialization and send another broadcast (say MY_CUSTOM_BROADCAST). The second app has a broadcastreceiver (mentioned by tag in manifest) which listens for this custom broadcast. At boot time the first app receives the ACTION_BOOT_COMPLETED and sends the custom broadcast successfully. But my second app's receiver does not seem to receive it. I tried sending the custom broadcast using "am broadcast". Even then the second app's receiver does not receive it.

Then I started the second apps's main activity and then tried sending the broadcast using "am broadcast". Then the second app's receiver successfully received it.

Can someone help me as to why my receiver is not getting the custom broadcast at boot time? I am running this on 4.0.3 (ICS).

like image 963
Krishna Avatar asked Mar 01 '12 07:03

Krishna


People also ask

Does broadcast receiver work in background?

As an Android developer, you'll often run into the scenario where you need to perform tasks and display notifications for your app in the background. To retain battery power on our users device we are going to run background tasks using a broadcast receiver.

How do I know if my broadcast receiver is running?

A simple solution to this problem is to call the registerReceiver() in your Custom Application Class. This will ensure that your Broadcast receiver will be called only one in your entire Application lifecycle. Show activity on this post.

How does broadcast receive work?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

Does broadcast receiver run on main thread?

As the previous answers correctly stated onReceive will run on the thread it's registered with if the flavour of registerReceiver() that accepts a handler is called - otherwise on the main thread.


1 Answers

Apparently in Android 3.1+, apps are in a stopped state if they have never been run, or have been force stopped. The system excludes these apps from broadcast intents. They can be included by using the Intent.FLAG_INCLUDE_STOPPED_PACKAGES flag.

http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html

http://developer.android.com/sdk/android-3.1.html#launchcontrols

Also, I think you need the Intent.FLAG_ACTIVITY_NEW_TASK flag.

like image 116
Khasm Avatar answered Sep 17 '22 01:09

Khasm