Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 689
abidkhan303 Avatar asked Sep 18 '26 05:09

abidkhan303


2 Answers

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

like image 72
Yogesh Somani Avatar answered Sep 20 '26 20:09

Yogesh Somani


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.

like image 33
Timothée Jeannin Avatar answered Sep 20 '26 18:09

Timothée Jeannin