Tuesday, July 9, 2013

BOOT_COMPLETED BroadcastReceiver In Android

Android BootReciever


Many a time We need to perform some task when Device/Mobile finish it's booting process.
For Example giving notification that "SIM has been Changed" etc.

For this we need a Broadcast  receiver that should receive "BOOT_COMPLETED" broadcast. We must know  that when a device finishes booting Android System sends "BOOT_COMPLTED" broadcast.

Registering the BootReciever in android manifest file


<receiver android:name=".BootReceiver">
                        <intent-filter>
                                <action android:name="android.intent.action.BOOT_COMPLETED" />
                                <category android:name="android.intent.category.HOME" />
                        </intent-filter>
   </receiver>



and do not forget to add the following permission in manifest .

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

this permission is required to listen/reverie the BOOT_COMPLETED action



BootRecieAndroidvr In

BootReceiver.java  

 

public class BootReceiver extends BroadcastReceiver
{
      
            public void onReceive(Context context, Intent intent)
            {
               
                   // Your code to execute when Boot Completd

                   Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show();
            }
}
                     
           
The onRecieve() method of BootReceiver will execute when boot completes, so wee need to write the code inside onReceive() method.





No comments:

Post a Comment