PendingIntent in Widget + TaskKiller
I have developed an application (called Instant Buttons) and the application has a widget function. This widget uses the PendingIntent for the onClick widget.
My PendingIntent code looks something like this:
Intent active = new Intent(context, InstantWidget.class);
active.setAction(String.valueOf(appWidgetId));
active.putExtra("blabla", blabla); //Some data
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
actionPendingIntent.cancel();
actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button, actionPendingIntent);
OnReceive receives an intent and does some things with the MediaPlayer class to play audio.
Some users have reports that widgets stop working after a while, and with some research I have found, this is because Task Killers. It looks like when you kill the app in TaskKiller the PendingIntent is erased from memory, so when you click on the widget it doesn't know what to do.
Is there any solution for this? Is my code wrong or something like the default PendingIntent behavior? Is there something I can use to avoid TaskKiller to stop my widgets from running?
Hey.
a source to share
Is there any solution for this?
Ask your users not to use task killers. Or, wait for some future Android release to close the task killer loophole.
Is my code wrong or something like the default PendingIntent behavior?
Your code appears to be fine. Killer burglars use napkins in almost everything, so I'm not in the least surprised by this behavior.
Is there something I can use to avoid TaskKiller to stop my widgets from working?
Not really. Your users - those who are not absolute idiots, at least - will hopefully learn to better understand the implications of their application of task killers. There is currently no reasonable defense against a task killer app.
a source to share
I had the same issue with pending intents in widgets no longer bind to their actions when the app was terminated. Create, undo, and re-create are handled, but you can also specify an undo flag so you don't have to explicitly undo yourself as the system does it for you. When you create your pending intent instead of using
PendingIntent.getBroadcast(context, 0, active, 0);
use
PendingIntent.getBroadcast(context, 0, active, PendingIntent.FLAG_CANCEL_CURRENT);
a source to share