NAVIGATION - SEARCH

Android - How To Auto Launch Your App

There are at times when you may need your android application to start up whenever you restart the device. For e.g. your App listens to location updates and when the device restarts, you need to register for location updates. This article will explain the process of auto launching an app whenever the device reboots and can be done very easily through simple steps. The concept of broadcast receiver is used to get this done and we need to register BOOT_COMPLETED broadcast which will in turn start a service to register for location updates. You may write any program logic in this service as per you app needs.

First of all we need to declare the required permission, the broadcast receiver and the service inside our Android manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.youritbuddy.autolauchapp"
	android:versionCode="1"
	android:versionName="1.0" >
	
	<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
	
	<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=".MainActivity"
			android:label="@string/title_activity_main" >
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		
		<receiver 
			android:name=".BootReceiver"
			android:enabled="true"
            android:exported="false" >
			<intent-filter>
				<action android:name="android.intent.action.BOOT_COMPLETED" />
				<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</receiver>
		
		<service android:name=".LocUpdates" android:enabled="true" android:exported="false" />
	</application>
</manifest>

Next step is to add a new class with the name BootReceiver to our package and ensure that it extends the BroadcastReceiver base class. Whenever the device boots, the OnReceive() method is fired which invokes the service.

package com.youritbuddy.autolauchapp;

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

public class BootReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
			Intent svcIntent = new Intent(context, LocUpdates.class);
			context.startService(svcIntent);
		}
	}
}

The last step here is to write code for the LocUpdates service which will register for location updates. This is just an example to register for location updates and you may write whatever logic that is required within your app which needs to be executed everytime the device boots up.

package com.youritbuddy.autolauchapp;

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

public class LocUpdates extends Service {
{

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Toast.makeText(this, "Service Started After Device Reboot", Toast.LENGTH_LONG).show();
		//PLEASE WRITE YOUR LOCATION UPDATES CODE OR ANY OTHER LOGIC HERE
	}
}

Please note that there are some device manufacturers like Xiaomi which blocks auto start of apps. For this they have security apps where you may need to enable or allow your app to auto start.

Add comment