0

I've an issue with an Alarm Manager. I wan't to execute my service each hour. Alarm Manager is launched after reboot and work well, even if the app is not open or open and closed (My PhoneStartReceiver call launchBackgroundService one time, after a completed boot). My problem is when I launch application after installation, without phone reboot. In this case, AlarmManager is killed when application is force closed or destroyed.

Problem is juste between installation, and next reboot. How to maintain AlarmManager enabled until next reboot ?

        <receiver
        android:name=".helpers.PeriodicalServiceCaller"
        android:process=":remote"/>
<receiver
        android:name=".helpers.PhoneStartReceiver"
        android:process=":remote">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

Here is my launchBackgroundServiceMethod, called in the both cases.

public static void launchBackgroundService(){
    // Construct an intent that will execute the PeriodicalServiceCalle

    Intent intent = new Intent(getApplicationContext(), PeriodicalServiceCaller.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Create a PendingIntent to be triggered when the alarm goes off
    final PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), PeriodicalServiceCaller.REQUEST_CODE,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Setup periodic alarm every minute
    long firstMillis = System.currentTimeMillis(); // alarm is set right away
    AlarmManager alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

    // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000L, pIntent);

}

PeriodicallServiceCaller code

public class PeriodicalServiceCaller extends BroadcastReceiver {

public static final int REQUEST_CODE = 12345;

// Triggered by the Alarm periodically (starts the service to run task)
@Override
public void onReceive(Context context, Intent intent) {

        Log.i("START-SERVICE", "PeriodicalServiceCaller");

        Intent i = new Intent(context, MonitorDataService.class);
        context.startService(i);
}

EDIT My launchBackgroundService is launch by an Acitivity if it's after install and by PhoneStartReceiver if it's after a reboot

  • Possible duplicate of [AlarmManager does not work when app is force closed](http://stackoverflow.com/questions/16401447/alarmmanager-does-not-work-when-app-is-force-closed) – Shaishav Sep 27 '16 at 13:21

2 Answers2

0

You need to register a BroadcastReceiver to detect when your are has been updated.

<receiver android:name=".MyBroadcastReceiver">
   <intent-filter>
     <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
   </intent-filter>
</receiver>

Take a look at

How to know my Android application has been upgraded in order to reset an alarm?

Community
  • 1
  • 1
Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
  • I don't think OPs query is related to this use case since, he mentioned: *AlarmManager is killed when application is force closed or destroyed.* – Shaishav Sep 27 '16 at 13:38
  • "Problem is juste between installation, and next reboot." so this will actually hook up right after installation and enable the alarm manager. Next time it reboots, the ON BOOT broadcast receiver will initiate the alarm manager. – Robert Estivill Sep 27 '16 at 13:44
  • Is it possible to launch the Alarm Manager in java, like if it was manifest which launch it ?So that it shouldn't be killed when app is killed – Mathieu Sachet Sep 27 '16 at 13:49
  • That's not the original question. You should read about the Application lifecycle and the AlarmManager cancellations in case the app is force closed. http://stackoverflow.com/questions/4643850/android-alarm-is-cancelled-after-closing-the-application – Robert Estivill Sep 27 '16 at 13:53
  • Not the same question but same problem, Alarm Manager is not canceled when launched by manifest (after a reboot) and canceled when launched after install/before reboot. Do you have an idea how to skirt the problem ? – Mathieu Sachet Sep 27 '16 at 14:10
  • If you want the Alarm manager to cancel all the alarms after boot, then you should register a broadcast receiver on_boot and remove your pending intents from the alarm manager. – Robert Estivill Sep 27 '16 at 14:13
  • No, this is the contrary, I want that it keep alive ! – Mathieu Sachet Sep 27 '16 at 14:14
  • You just wrote "Alarm Manager is not canceled when launched". ?? Anyway, if you want to start with the boot, then register a broadcast receiver to the BOOT_COMPLETED event and set up your alarms there. http://stackoverflow.com/questions/12034357/does-alarm-manager-persist-even-after-reboot – Robert Estivill Sep 27 '16 at 14:18
0

Is launchBackgroundService in a service or run from the activity? If it is run from the service please check this answer Background Service getting killed in android

START_STICKY was the thing I missed.

Community
  • 1
  • 1
MaTePe
  • 936
  • 1
  • 6
  • 11