1

I'm trying to make a sticky service, which restarts itself after a short time after dying (for example, because Android has killed it to free the memory). (Un)fortunately I'm on Android 5.0.1 which has a beautiful bug of memory leak and makes real testing easy.

Looking at logcat I see:

I/ActivityManager(  943): Process com.app.my (pid 28834) has died
W/ActivityManager(  943): Scheduling restart of crashed service com.app.my/com.service.my in 191688ms
I/ActivityManager(  943): Process com.app.my (pid 30842) has died
W/ActivityManager(  943): Scheduling restart of crashed service com.app.my/com.service.my in 766752ms

later the time is rising to 3M making it inacceptable.

I've tried to fix that using AlarmManager and restart intent:

    Intent restartIntent = new Intent(activity, activity.getClass());
    restartIntent.putExtra("config", config);
    PendingIntent restartAct = PendingIntent.getActivity(
       activity.getApplicationContext(), 0, restartIntent, PendingIntent.FLAG_NO_CREATE);
    AlarmManager alarmManager = (AlarmManager)activity.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60000, 60000, restartAct);

However this didn't help. I can't find any information in log, so maybe AlarmManager repeating is abandoned after activity has died or perhaps I've done something incorrectly?

I was observing WhatsApp application and I've found:

I/ActivityManager(  943): Process com.whatsapp (pid 27009) has died
W/ActivityManager(  943): Scheduling restart of crashed service com.whatsapp/.messaging.MessageService in 10947ms
I/ActivityManager(  943): Process com.whatsapp (pid 29913) has died
W/ActivityManager(  943): Scheduling restart of crashed service com.whatsapp/.messaging.MessageService in 10918ms
I/ActivityManager(  943): Process com.whatsapp (pid 30681) has died
W/ActivityManager(  943): Scheduling restart of crashed service com.whatsapp/.messaging.MessageService in 10987ms

The first and the last crash was exactly at the same time as com.app.my process. Somehow WhatsApp manages to keep the "retry time" at the same level despite frequent crashes. How does they do that? The answer here suggests it should be the same for all aps: Crashed service restarted after a very long time

Remarks:

Community
  • 1
  • 1
piotrwest
  • 2,098
  • 23
  • 35

1 Answers1

1

Check that:

Intent watchdogIntent = new Intent(getApplicationContext(), SomeClasss.class);
    watchdogIntent.setAction(INTENT_ACTION);

    ((AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE))
            .setInexactRepeating(
                    AlarmManager.ELAPSED_REALTIME,
                    0,
                    currentWatchDogInterval,
                    PendingIntent.getService(
                            getApplicationContext(),
                            0,
                            watchdogIntent,
                            0)
            );
Rafal
  • 292
  • 2
  • 10
  • What does this action do: "DEFAULT_TRIP_START_INTENT_ACTION"? – piotrwest May 09 '16 at 20:48
  • @piotrwest I changed in code to: INTENT_ACTION and it's action passed with Intent send defined by you. – Rafal May 09 '16 at 20:52
  • Ok, but here you try to schedule service creation, not process/activity creation, right? This way you will get message "F/ProcessStats Starting service ServiceState{abc com.app.my pkg=com.app.my proc=abc} without owner". – piotrwest May 10 '16 at 20:05