Monday, October 22, 2012

Start Application at Boot-up

So, I needed to add an autostart for the application for these Kiosks.

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

This goes below your <application> tag.

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

Then you add a class named "StartMyServiceAtBootReceiver" in your src folder of your Android Application project.  This is the code you should add for it.. assuming your main activity class is named: "MainActivity".

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}