abidkhan303
abidkhan303

Reputation: 1791

android services life time

My question is.

In Android is there a way to create a service which can stay alive even you restart the mobile phone, until it does not perform its task,

For example alarm application. If you restart your mobile it will be triggered without any problem.

In android All services behave like this or do we have some solution for that?

Kindly explain in detail.

Upvotes: 3

Views: 2349

Answers (4)

sleep
sleep

Reputation: 4954

Check out the Services documentation. You want the START_STICKY flag which will make the system restart your service if it is ever terminated due to low memory etc.

To handle device restart...

Manifest

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


    <service
        android:name=".BootService"
        android:label="Boot Service" >
        <intent-filter>
            <action android:name="com.my.package.BootService" />
        </intent-filter>
    </service>

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

Source

public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(this.getClass().getName(), "Boot Receiver onReceive()");
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) 
    {
        // Fire up your service.
        Intent serviceIntent = new Intent("com.my.package.BootService");
        serviceIntent.putExtra(Constants.BOOT_LAUNCH_EXTRA, true);
        context.startService(serviceIntent);
    }
}

}

public class BootService extends IntentService {

public BootService()
{
    super("BootService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.d(this.getClass().getName(), "Boot Service onHandleIntent()");
    if(intent.hasExtra(Constants.BOOT_LAUNCH_EXTRA))
    {
                 //...
    }
}

}

Upvotes: 0

Timoth&#233;e Jeannin
Timoth&#233;e Jeannin

Reputation: 9912

is there a way to create a service which can stay alive even you restart the mobile phone

No. All running services are killed when turning off a phone.

For example alarm application. If you restart your mobile it will be triggered without any problem.

Yes, but it's not because the service stayed alive. It's because the alarm app respond to the android.intent.action.BOOT_COMPLETED intent.

What you can do is creating a BoradcastReceiver that responds to this intent and that start your service.

The problem is that the user can kill this service manually. If you want to build an alarm clock, you should not have a service always running in the background.

You should make use of the AlarmManager and PendingIntent.

Something like in your manifest :

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

<receiver
    android:name=".broadcasts.InitReceiver"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.TIME_SET" />
        <action android:name="android.intent.action.TIMEZONE_CHANGED" />
    </intent-filter>
</receiver>

And something like this for the broadcast.

public class InitReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

            // Schedule your alarms again.

    }
}

And you should schedule alarms like this :

Intent intent = new Intent(context, Ring.class);
intent.setData(alarmUri);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, ringTime, pendingIntent );

Where Ring is the broadcast that handles the alarm ring.

Upvotes: 2

Yogesh Somani
Yogesh Somani

Reputation: 2624

It will take 2 steps:

(1) First Create a BroadcastReceiver and start the service in this receiver's onReceive() method:

  public class MyReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, YourService.class);
        context.startService(service);
     }
  } 

(2) Now decalre this receiver in manifest like this:

    <receiver android:name=".MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

This way your Service will always be running and will start even if the phone is restarted. For more details , refer to this link:

http://www.vogella.com/articles/AndroidServices/article.html

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007359

In Android is there a way to create a service which can stay alive even you restart the mobile phone, until it does not perform its task,

No, if we interpret your request literally. All processes are terminated when you restart the device, just as all processes are terminated when you turn off the device.

For example alarm application. If you restart your mobile it will be triggered without any problem.

That is because the "alarm application" is getting control at boot time, via an ACTION_BOOT_COMPLETED BroadcastReceiver, and is rescheduling its alarm with AlarmManager.

Similarly, a service implementing a download queue would get control at boot time, via an ACTION_BOOT_COMPLETED BroadcastReceiver, at which time it would determine what yet needs to be downloaded and return to that work.

Upvotes: 0

Related Questions