Saturday 16 April 2011

Android - Intents and Broadcast Receivers

Intents
Intents are messages that are passed between Android components. They are used to do specific jobs inside the Android platform. Most of the actions that are carried out in Android involve intents. For instance, you as an application developer should send intents for starting activities, starting / stopping services, completing an action using other applications like taking a photograph, sending email, text messages, etc.,

The primary pieces of an Intent are:
  1. Action - The general action to be performed
  2. Data - The data to operate on, generally a URI
You can also define your own actions and receive those intents using broadcast receivers.

Broadcast Receivers a.k.a Intent Receviers
Broadcast Receivers receive intents that sent by sendBroadcast(). You can statically publish a receiver in the AndroidManifest.xml using the <receiver> tag or you can do the same using Context.registerReceiver(). In this example we will be registering out broadcast receiver statically.

MainActivity.java
 package com.mobsandgeeks.ire;  
   
 import android.app.Activity;  
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
   
 public class MainActivity extends Activity implements OnClickListener {  
      private Button sendIntentButton;  
     
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
       
           sendIntentButton = (Button) findViewById(R.id.send_intent_button);  
           sendIntentButton.setOnClickListener(this);  
      }  
   
      public void onClick(View view) {  
           if(view == sendIntentButton) {  
                Intent i = new Intent();  
                i.setAction("com.mobsandgeeks.action.DEMO_ACTION");  //Our Action, use the same in the BroadcastReceiver too
                sendBroadcast(i);  //Broadcast the intent with the custom action
           }  
      }  
 }  

MyReceiver.java
 package com.mobsandgeeks.ire;  
   
 import android.content.BroadcastReceiver;  
 import android.content.Context;  
 import android.content.Intent;  
 import android.widget.Toast;  
   
 public class MyReceiver extends BroadcastReceiver {  
   
      @Override  
      public void onReceive(Context context, Intent intent) {  
           String action = intent.getAction();

           //This check is redundant since the intent-filter in the manifest only 
           //accepts our action. Use condition checking only when you use a receiver 
           //to handle multiple actions
           if(action.equals("com.mobsandgeeks.action.DEMO_ACTION")) {
                Toast.makeText(context, "Intent received", Toast.LENGTH_SHORT).show();  
           }  
      }  
 }  

AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.mobsandgeeks.ire"  
    android:versionCode="1"  
    android:versionName="1.0">  
   <uses-sdk android:minSdkVersion="4" />  
        
   <application android:icon="@drawable/icon" android:label="@string/app_name">  
     <activity android:name=".MainActivity" android:label="@string/app_name">  
          <intent-filter>
               <action android:name="android.intent.action.MAIN" />  
               <category android:name="android.intent.category.LAUNCHER" />  
          </intent-filter>  
     </activity>  
       
     <receiver android:name="MyReceiver">  
          <intent-filter>  
               <action android:name="com.mobsandgeeks.action.DEMO_ACTION" />  
          </intent-filter>  
     </receiver>  
   </application>  
 </manifest>  

Source Code
You can download the source from here.

Screenshot

No comments:

Post a Comment