Saturday, 29 August 2015

Broadcast receivers in android

BROADCAST RECEIVER

Android broadcastreceiver is also a component where you can register for system or application events. You will be notified about the events after registering.

There are following two important steps to make BroadcastReceiver works
1.Creating the Broadcast Receiver.
2.Registering Broadcast Receiver

Creating the Broadcast Receiver
A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }

}

Registering Broadcast Receiver
An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file. 

  <receiver android:name="MyReceiver">
      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED">
         </action>
      </intent-filter>  

   </receiver>

There are several system generated events defined as final static fields in the Intent class. The following table lists a few important system events.

android.intent.action.BATTERY_CHANGED           
Sticky broadcast containing the charging state, level, and other information about the battery.
android.intent.action.BATTERY_LOW      
Indicates low battery condition on the device.
android.intent.action.BATTERY_OKAY    
Indicates the battery is now okay after being low.
android.intent.action.BOOT_COMPLETED            
This is broadcast once, after the system has finished booting.
android.intent.action.BUG_REPORT        
Show activity for reporting a bug.
android.intent.action.CALL          
Perform a call to someone specified by the data.
android.intent.action.CALL_BUTTON     
The user pressed the "call" button to go to the dialer or other appropriate UI for placing a call.
android.intent.action.DATE_CHANGED  
The date has changed.
android.intent.action.REBOOT   
Have the device reboot.

Example:
Here is my sample code using broadcast receiver with android.intent.action.PHONE_STATE to track phone incoming call status.

1.Register the broadcast receiver


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiverexample"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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="com.example.broadcastreceiverexample.IncomingcallReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>
</manifest>


2.Create broadcast receiver


public class IncomingcallReceiver extends BroadcastReceiver{
 Context context;  
      @Override  
      public void onReceive(Context context, Intent intent){  
          try{  
           String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);  
              if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){  
                   Toast.makeText(context, "Phone is ringing", Toast.LENGTH_LONG).show();  
              }                  
              if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){  
                   Toast.makeText(context, "Call recieved", Toast.LENGTH_LONG).show();  
              }                  
              if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){  
                   Toast.makeText(context, "Phone is idle", Toast.LENGTH_LONG).show();  
              }  
          }  
          catch(Exception e){e.printStackTrace();}  
      }  
}



DOWNLOAD SAMPLE CODE

No comments: