Question

I'm having this problem but I didn't found any way to fix the problem.

I'm building a service, that have to make some things at especific time, so I have an AlarmManager to count the timer.

I'm starting the service from Activity, via getBaseContext().startService(intent), I have the AlarmManager as mAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + TIME, mPendingIntent);

The PedingIntent is: mPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0,  new Intent(filter), 0);

I'm registering the local receiver as the following: registerReceiver(mReceiver, new IntentFilter(filter));

The problem I'm facing is: the service run normally, no errors , but if I remove my app from "Recent apps", the service remain running until the AlarmManager trigger my PendingIntent. It crashes, after a random time (can be seconds or minutes) it restarts and doesn't crash anymore.

The service is running on foreground.

Remember: the service doesn't crash immediately, it crashes only when my AlarmManager trigger my PendingIntent (that should call my BroadcastReceiver).

I don't know why it happens, is there some flags I could set on my PendingIntent or something?

Lets say that the service "survived" after all of this I wrote above, if I just open my app and leave it an then remove my app from "recents", my service crash again.

Is there another way I could use to do timer tasks that wakes up the device, as well as the AlarmManager does?

Is there any listener event or Intent action that I could filter when I remove my app from recent window or something like this? Something like isAppRemovedFromRecents() or isAppTrullyDiesFromTheSystem(). This way I could use a receiver to restart my service.

I also thought about making a second app only for the service, this way I would avoid all of this problem, but it doesn't look "right".

What could I do ?

EDIT

value of the filter: String filter = "com.mypackage.TIMERFILTER";

LogCat: Scheduling restart of crashed service com.mypackage/.MyService in 5000ms

Thanks in advance

Was it helpful?

Solution

I found a solution, after doing a lot of research, I finally came up here

The error was on the PendingIntent, as I thought.

There is some bug or undocumented change on Android API, since API 16 (Jelly Bean). Instead of this:

final String filter = "MYFILTER"

final PendingIntent mPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0,  new Intent(filter), 0);

You should do this:

final String filter = "MYFILTER"

final Intent piIntent = new Intent(filter);


if(Build.VERSION.SDK_INT >= 16) piIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);


final PendingIntent mPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 1, piIntent, 0); // requestCode must be 1, not 0

This way you can start the service, leave the app and swype it out from "recent apps" and the service won't crash.

Tested on Motorola RAZRi, android 4.1.2, I don't know if it will work on older android versions, please test and share it here : )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top