Wednesday, May 18, 2011

Starting an Android Service at Boot time

Always a service is started by us manually that means it was not start by default. If we switch off the mobile then all the running services will stop.

For example if we are running a service. If we reboot the mobile then all the running services are stopped. So we have to run the service again.

By default android broadcasts a intent (android.intent.action.BOOT_COMPLETED)at the time of booting. So we have to receive that and in that we again start a service.

This should be accomplished by using Broad Cast Receiver.

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

public class BootBroadcastReceiver extends BroadcastReceiver
public void onReceive(Context context, Intent intent)
Log.i("Receiver Called.", "start the service.");
We have to add this receiver in the manifast.xml. The code is as follows.
<!-- Reciver -->
<receiver android:name=".BootBroadcastReceiver">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />

No comments:

Post a Comment