Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Autostart service after device reboot in android

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.36k
    Comment on it

    Sometime you need to register your own android application or service to start automatically when the device has been Rebooted to perform particular task. Here we will learn the simplest way to start an Android service or an application at device boot up.

    It's quite easy.
    First you need to add Add the permission in AndroidManifest file to catch the boot complete event.

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

    Then you need to register a BroadcastReveiver. We call it BootCaptureIntentReceiver. Your AndroidManifest file will looks like this below.

     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example"> 
     <uses-permission
     android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
     <application>   
    <receiver android:name=".BootCaptureIntentReceiver">
     <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />    
     </intent-filter> 
     </receiver>  
     <service android:name=".StartServiceOnBoot"/> 
     </application> 
     </manifest>
    

    As the last step you need to implement the Receiver. This receiver will catch the device reboot action and just starts your background service again.

    import android.content.Context;
    import android.content.BroadcastReceiver;
    import android.content.Intent;
    
    // here is the OnRevieve methode which will be called when boot completed
    public class BootCaptureIntentReceiver extends BroadcastReceiver{
         @Override
         public void onReceive(Context context, Intent intent) {
     //we double check here for only boot complete event
     if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
       {
         //here we start the service  again.           
         Intent serviceIntent = new Intent(context, StartServiceOnBoot.class);
         context.startService(serviceIntent);
       }
     }
    }
    

    Hope it might help you. Thanks :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: