Android AutoStart App after boot with example


Introduction
             Sometimes we may need our android application to start up when the device boots up. This can be done very easily through simple steps as explained in this tutorial.
To get this done,  all we need is a broadcast receiver to receive boot complete broadcast  which will in turn start a service to run in the background and their corresponding permissions in the Android Manifest file.

Step 1 : Create Broadcast receiver
          Create a broadcast receiver class that extends BroadcastReceiver. OnReceive() method
will be called when device boot is complete and inside which we will call Service as shown below


public void onReceive(Context context, Intent intent) 
{
    if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) 
      {
 Intent serviceIntent = new Intent(context, AutoStartUp.class);
 context.startService(serviceIntent);
      }
}

Step 2 : Create a Service
             This service will get triggered from the broadcast receiver. OnCreate() method will be invoked when the service is triggered. Inside which a Toast will be displayed as shown below

 public void onCreate() {
  super.onCreate();
  Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
  // do something when the service is created
 }

Step 3 : Add required permissions in AndroidManifest.xml
              We need to add permissions for boot complete event, register broadcast receiver and service in Android Manifest

Permissions
This permission will allow our application to send and receive broadcasts to notify our phone is booted.


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

Register Broadcast receiver and Service

 <receiver
            android:name=".BootComplete"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name=".AutoStartUp" >
        </service>

Now go ahead and Reboot your device, once boot is completed you should be able to see the below screen shot


Thats it.. We are done.. It is so simple to auto start Android apps during boot up.

BootComplete.java


import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;

public class BootComplete extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {

  if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
   Intent serviceIntent = new Intent(context, AutoStartUp.class);
   context.startService(serviceIntent);
  }
 }

}

}

AutoStartUp.java


import android.app.Service;
import android.os.IBinder;
import android.widget.Toast;
import android.content.Intent;

public class AutoStartUp extends Service {
 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {
  super.onCreate();
  Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
  // do something when the service is created
 }

}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidautostartup"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name=".BootComplete"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name=".AutoStartUp" >
        </service>

        <activity
            android:name="com.example.androidautostartup.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>
    </application>

</manifest>

Source Code of this Application
AndroidAutoStartUp.zip





Reactions:

0 comments :

Post a Comment