Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast receiver not receiving intent

I have two apps that I have complete control over. Both are signed with the same cert and both use the exact same intent filter. One sends the broadcast from a fragment, the other is suppose to receive it and do something. This however is not working:

Strings.FILTER_INIT_REGISTER = "com.app.FILTER_INIT_REGISTER"

Intent intent = new Intent(Strings.FILTER_INIT_REGISTER);
getActivity().sendBroadcast(intent);

I have registered the receiver in the Manifest app tag for the app containing the ReportingReceiver class:

<receiver             
    android:name=".receivers.ReportingReceiver"
    android:exported="true"
    >
        <intent-filter>
            <action android:name="com.app.FILTER_INIT_REGISTER" />
            <category android:name="android.intent.category.DEFAULT" />                
        </intent-filter>
</receiver>

Curious why the ReportingReceiver class is not getting the intent call?

like image 385
Roy Hinkley Avatar asked Oct 08 '12 16:10

Roy Hinkley


People also ask

How does broadcast receiver Intent work?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

What is the difference between intent and broadcast receiver?

An intent is a messaging object, a broadcast receiver is an app component. An intent is used to request some action from some app component, it could be a broadcast receiver, an activity or a service.


2 Answers

If your application only has a service and receivers then this won't work in Android 3.1 and later. The reason is that the system will not send broadcast Intents to application that are in the STOPPED STATE. An application is in the STOPPED STATE when it is first installed. It is removed from the STOPPED STATE when the user manually starts the application for the first time. It is returned to the STOPPED STATE if the user forces the application to stop using the application manager tool.

Since your application has no Activities, there is no way for the user to "start" it. Therefore it will never come out of the stopped state.

See http://developer.android.com/about/versions/android-3.1.html#launchcontrols

like image 73
David Wasser Avatar answered Oct 16 '22 11:10

David Wasser


As Android Addict says in his comment to David Wasser's answer ... there is a way around this behaviour.

Just add the following flag to the calling Intent. This will ensure that you also reach broadcast receivers from "stopped" applications.

http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES

You can read more about this Android 3.1 change here

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

and here

http://code.google.com/p/android/issues/detail?id=18225

like image 36
forgemo Avatar answered Oct 16 '22 13:10

forgemo