小部件中的 PendingIntent +任务杀手

发布于 2024-08-29 18:50:46 字数 856 浏览 4 评论 0原文

我开发了一个应用程序(称为“即时按钮”),该应用程序具有小部件功能。该小部件使用 PendingIntent 作为小部件的 onClick。

我的 PendingIntent 代码是这样的:

    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 获取意图并使用 MediaPlayer 类执行一些操作来重现声音。

我收到一些用户的报告,这些小部件在一段时间后停止工作,经过一些研究,我发现这是因为任务杀手。似乎当您在 TaskKiller 中杀死应用程序时,PendingIntent 会从内存中删除,因此当您单击小部件时,它不知道该怎么做。

有什么解决办法吗?我的代码是错误的还是什么或者它是 PendingIntent 的默认行为?有什么我可以用来避免 TaskKiller 停止我的小部件工作吗?

问候。

I've developed an Application (called Instant Buttons) and the app has a widget feature. This widget uses PendingIntent for the onClick of the widget.

My PendingIntent code is 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);

The onReceive gets the intent and do some stuff with the MediaPlayer class to reproduce a sound.

I have reports from some users that the widgets stop working after a while and with some research i've discovered is because the Task Killers. It seems that when you kill the app in the TaskKiller, the PendingIntent is erased from memory, so when you click the widget, it doesn't know what to do.

Is there any solution for this? Is my code wrong or something or it's the default behavior of the PendingIntent? Is there something I can use to avoid the TaskKiller to stop my widgets from working??

Greetings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

z祗昰~ 2024-09-05 18:50:46

有什么解决办法吗?

要求您的用户不要使用任务杀手。或者,等待未来的 Android 版本来堵住任务杀手漏洞。

我的代码是错误的还是什么?
的默认行为
待定意图?

您的代码大概没问题。任务杀手使用的黑客技术几乎消灭了所有东西,所以我对这种行为一点也不感到惊讶。

有什么我可以用来避免的吗?
TaskKiller 来阻止我的小部件
工作?

并不真地。你的用户——至少那些不是白痴的用户——有望学会更多地意识到他们使用任务杀手的影响。目前,还没有针对任务杀手应用程序的合理防御。

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 or it's
the default behavior of the
PendingIntent?

Your code is presumably fine. The hack the task-killers use wipes out pretty much everything, so I'm not the least bit surprised at this behavior.

Is there something I can use to avoid
the TaskKiller to stop my widgets from
working?

Not really. Your users -- those who aren't total morons, at least -- will hopefully learn to be more aware of the impacts of their use of task-killers. At present, there is no sensible defense against a task killer app.

岁月苍老的讽刺 2024-09-05 18:50:46

当应用程序被关闭时,我也遇到了同样的问题,小部件中的未决意图不再连接到其活动。创建、取消和重新创建都有效,但您也可以指定取消当前标志,这样您就不必自己显式调用取消,因为系统会为您执行此操作。
当您创建待定意图而不是使用

PendingIntent.getBroadcast(context, 0, active, 0);

use时

PendingIntent.getBroadcast(context, 0, active, PendingIntent.FLAG_CANCEL_CURRENT);

I had this same problem with pending intents in a widget no longer connecting to their activities when the app had been dismissed. Creating, canceling, and recreating worked, but you can also specify a cancel current flag so that you do not have to explicitly call cancel yourself as the system will do it for you.
When you are creating your pending intent instead of using

PendingIntent.getBroadcast(context, 0, active, 0);

use

PendingIntent.getBroadcast(context, 0, active, PendingIntent.FLAG_CANCEL_CURRENT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文