Android SDK: Add application to the autostart

Add your Android application to the autostart

To add an Android application to the device’s autostart is actually pretty easy. But if some minor but important settings are wrong the autostart function doesn’t work. Here comes a fully functional example which opens your Android application after the device has finished its booting sequence.

  • Create a new Android application in Eclipse called „OnBootCompletedExample“. The namespace is on.boot.completed.
  • Leave the main Activity as is:

    OnBootCompletedExampleActivity.java

    import android.app.Activity;
    import android.os.Bundle;
    
    public class OnBootCompletedExampleActivity extends Activity {
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    	}
    }
    
  • Create a new class called Autostart.java which extends BroadcastReceiver:

    Autostart.java

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class Autostart extends BroadcastReceiver {
    
    	/**
    	 * Listens for Android's BOOT_COMPLETED broadcast and then executes
    	 * the onReceive() method.
    	 */
    	@Override
    	public void onReceive(Context context, Intent arg1) {
    		Log.i("Autostart", "BOOT_COMPLETED broadcast received. Executing following code:");
    
    		Intent intent = new Intent(context, StarterService.class);
    		context.startService(intent);
    	}
    }
    

    In the AndroidManifest.xml file we are going to add this class as a receiver. This class will listen to the broadcast call the Android OS sends after the boot sequence has finished (meaning after the phone started up).

  • Create another class called StarterService.java which will extend Service:

    StarterService.java

    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    import android.widget.Toast;
    
    public class StarterService extends Service {
    	private static final String TAG = "MyService";
    
    	@Override
    	public IBinder onBind(Intent intent) {
    		return null;
    	}
    
    	@Override
    	public void onDestroy() {
    		Toast.makeText(this, "My Service stopped", Toast.LENGTH_LONG).show();
    		Log.d(TAG, "onDestroy");
    	}
    
    	/**
    	 * The started service opens the Activity.
    	 */
    	@Override
    	public void onStart(Intent intent, int startid) {
    		Intent intents = new Intent(getBaseContext(), OnBootCompletedExampleActivity.class);
    		intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    		startActivity(intents);
    
    		Toast.makeText(this, "My Service started", Toast.LENGTH_LONG).show();
    		Log.d(TAG, "onStart");
    	}
    }
    

    When the class Autostart receives the BOOT_COMPLETED broadcast from Android OS it will start the StarterService which then starts the Android Activity „OnBootCompletedExampleActivity“.

  • And finally we update the AndroidManifest.xml with the changes we need to do in order for the application to work how we want:

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="on.boot.completed"
        android:installLocation="internalOnly"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="7" />
    
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name="on.boot.completed.OnBootCompletedExampleActivity"
                android:label="@string/app_name"
                android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver android:name="on.boot.completed.Autostart" >
                <intent-filter>
                    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
    
            <service
                android:name="on.boot.completed.StarterService"
                android:enabled="true"
                android:exported="true" />
        </application>
    
    </manifest>
    

    A few notes about AndroidManifest.xml:

    • It’s very important to add
      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

      before the <application> tag.

    • If the app is installed on the SD Card the autostart will not work! That’s why it’s important that we add
      android:installLocation="internalOnly"

      after package=“on.boot.completed“

    • Also add
      <action android:name="android.intent.action.QUICKBOOT_POWERON" />

      in addition to

      <action android:name="android.intent.action.BOOT_COMPLETED" />

      to the receiver. Some HTC devices won’t catch the BOOT_COMPLETED broadcast.

That’s all. Now build and run your app. After it has been started turn off your phone and turn it back on and the app should start automatically after the device has booted up.

Here you can download the working Android project:
OnBootCompletedExample

One Response to Android SDK: Add application to the autostart

  1. Ravi sagt:

    Awesome website. Thanks for your code. it mean lot to u.

Schreibe einen Kommentar