Wednesday, April 13, 2011

Auto start a service with Intent of "android.intent.action.BOOT_COMPLETED" action

Last exercise "Start a service to send Notification" show how to start a service by user clicking in a activity. In this exercise, a BroadcastReceiver will be implemented to receive Intent of "android.intent.action.BOOT_COMPLETED" action after power-up, no interactive with user.

"android.intent.action.BOOT_COMPLETED" is Broadcast Action, is broadcast once, after the system has finished booting. It can be used to perform application-specific initialization. You must hold the RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.

Create a new class AutoStartNotifyReceiver.java, extends BroadcastReceiver. Simple do the same thing as that in Start button OnClickListener in last exercise "Start a service to send Notification", start a service.
package com.exercise.AndroidNotifyService;

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

public class AutoStartNotifyReceiver extends BroadcastReceiver {

private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
Intent myIntent = new Intent(context, com.exercise.AndroidNotifyService.NotifyService.class);
context.startService(myIntent);
}

}

}


In order to receive Intent of "android.intent.action.BOOT_COMPLETED", modify AndroidManifest.xml to add <receiver> for AutoStartNotifyReceiver, also grant permission of "android.permission.RECEIVE_BOOT_COMPLETED".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidNotifyService"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidNotifyService"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".NotifyService"/>
<receiver android:name=".AutoStartNotifyReceiver" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
</application>
</manifest>


Keep using the other files implemented in last exercise "Start a service to send Notification".

Download the files.

2 comments:

Unknown said...

your post are excelent and i used to
ground in android world thanks so much.

Unknown said...

If I have a Button "Stop Service" and push it, the service will stop, but it will start after boot anyway (and I don't want it).
How can I manage this?
Thanks