Monday, July 8, 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.





7 comments:

  1. As per my understanding this mechanism works only if we have started application once. If we haven't started application not even once then "BootReceiver" function will not be called. Could you please suggest what should we do in that case so that broadcast receiver can receive BOOTUP_COMPLETE event. Thanks

    ReplyDelete
    Replies
    1. BOOT_COMPLETED is only recieved when device completes BOOT process.

      Delete
  2. Thank you, but since i have implemented the broadcast receiver my app crashes. I have the right permission and i works on wifi but not on 3G mode. Logcat says: Cannot obtain root - Caused by: java.io.IOException: Permission denied. I never access anything that needs root permissions, i just call a service and as i said it works perfectly fine on wifi.

    ReplyDelete
    Replies
    1. Can you please post the full Log Cat detail, may be then only I can help you.

      Delete
  3. Hii guys please check my code i have been trying to run but boot receiver is not responding how ever i try












    My Boot Receiver

    public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

    Intent i = new Intent();
    i.setClassName("com.talentcodeworks.callrecorder", "com.talentcodeworks.callrecorder.callrecorder");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
    Toast.makeText(context,"BOOT",Toast.LENGTH_SHORT).show();

    }

    }
    }

    I cannot figure it out why is this not working on my vivo android version 5.0

    ReplyDelete