Android Broadcast Receiver – Change in flow since API 3.1

Prior to API 3.1 we could have a broadcast receiver which could have been invoked by an implicit intent even if the application to which it belonged was in stopped state.

But this posed a security threat. Hence Google made it mandatory that for any broadcast receiver to receive an intent, there should be an activity and the application should not be in stopped state. Here is the link for further reading.

When an application is launched, it is in stopped state and hence it mandates the user to activate the application which has the broadcast receiver. If the application is force stopped by the user, then again the broadcast receiver fails to receive the intent. Hence apk having only broadcast receiver and developed on version prior to 3.1 will no longer work for later versions.
However one can make use of FLAG_INCLUDE_STOPPED_PACKAGES to activate components in stopped application. This will not require creation of another activity in order to use the broadcast receiver.

Intent intent = new Intent("com.custom.intent"); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); 
this.sendBroadcast(intent);

The other way as I had mentioned earlier would be to write an activity which may or may not be a launcher activity.

 
PackageManager pm = getPackageManager(); 
Intent appStartIntent = 
pm.getLaunchIntentForPackage("com.your.broadcast.receiver.package");
if (null != appStartIntent)
{ 
     startActivity(appStartIntent);
}

If it is not a launcher app, then you need to mention the the category as INFO in the intent-filter in the manifest file of the broadcast receiver.

 
<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" <
       <activity
            android:name=".InvokedActivity"
            android:label="@string/app_name"<
            <intent-filter<
                <action android:name="android.intent.action.MAIN" /<                         
               <category android:name="android.intent.category.INFO" / <
<-- If the activity is a launcher then mention this instead 
             <category android:name="android.intent.category.LAUNCHER" /
                             < --<

             </intent-filter <
        </activity <
       <receiver android:name=".TestBroadCastReceiver" <
           <intent-filter <
           <action android:name="com.custom.intent" <
          </action <
        </intent-filter <
       </receiver <
<application <
    

Here is the code for BroadCastReceiver:

public class TestBroadCastReceiver extends BroadcastReceiver { 
 
    private static final String CUSTOM_INTENT = "com.custom.intent";
    
    public void onReceive(Context context, Intent intent) {
         if (intent.getAction().equals(CUSTOM_INTENT)) {
              //your code here 
         } 
    }
} 

The code for InvokedActivity:

 public class InvokedActivity extends Activity { 
      
       public void onCreate(Bundle b) { 
             super.onCreate(b); 
             /*Toast.makeText(this, "starting xyz receiver...blah blah :)", Toast.LENGTH_LONG) .show();*/
             finish();
      } 
}
  
  

Reference: Broadcast receiver – a change in flow since API 3.1 from our JCG partner Ashimita Adusumilli at the My small world of Android blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


five × = 5



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close