Android start application on boot

In this Android tip, I am going to show you how to start your application automatically when Android system completely booted. To achieve this goal, you need to register a broadcast receiver for the android.intent.action.BOOT_COMPLETED system event in the AndroidManifest.xml file. It requires the android.permission.RECEIVE_BOOT_COMPLETED permission. The android.intent.action.BOOT_COMPLETED system event is fired once the Android system has completed the boot process. You should note that the application has to be started at least one time before the application is able to receive the android.intent.action.BOOT_COMPLETED system event.


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.androidexample"
   android:versionCode="1"
   android:versionName="1.0" >
<uses-sdk
     android:minSdkVersion="8"
     android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
   <application
     android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme" >
     <activity
        android:name="com.example.androidexample.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>
     <receiver android:name="MyReceiver" >
        <intent-filter>
           <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
     </receiver>
   </application>

</manifest>


Then create a class called MyReceiver. The MyReceiver class extends the BroadcastReceiver class. You need to override onReceive() method to start an Activity when the event that the broadcast receiver was registered happens. To start the Activity, you need to provide the current context, and the Activity to be started when you construct the Intent object.

package com.example.androidexample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent starter_intent = new Intent(context, MainActivity.class);
   starter_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   context.startActivity(starter_intent);
}
}


Android-start-application-on-boot

Posted by: Dara | post date: 09-24-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.