Monday, June 3, 2013

Boot Reciever 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.





4 comments:

  1. The onRecieve() method of BootReceiver will execute when boot completes, so wee need to write the code inside onReceive() method.

    ReplyDelete
    Replies
    1. Yes.
      inside onReceive() method write the code you want to execute when boot completes like SIM has changed etc.

      Delete
  2. in case if we want to start the app immediately after booting completed and to start activity what need to be written in manifest file and how to call the intent from onReceive() method

    ReplyDelete
  3. how to check the internet connection status programatically not all other networks

    ReplyDelete