Android - AppWidget 的待处理意图不会触发
我有一个应用程序小部件,其布局中有一个按钮。 单击该按钮时,会触发一个调用我的广播接收器的意图。
它工作得很好,但有时,在使用任务管理器中的“清除内存”按钮后,小部件会卡住 - 单击它不会执行任何操作。但如果它正在运行,它仍然可以从我的应用程序接收更新。
我不确定未触发待处理意图是否是内存清除错误,还是我的错误。 不管怎样,代码如下:
注册挂起的意图(应用程序小部件的 onUpdate 方法)
Intent intent = new Intent(context, ServiceControl.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
views.setOnClickPendingIntent(R.id.appwidgetbutton, pendingIntent);
,然后使用视图更新小部件。
这是应用程序小部件提供商的声明:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:initialLayout="@layout/appwidget"
android:updatePeriodMillis="0">
</appwidget-provider>
我不希望系统调用小部件更新,我只从我的应用程序本身更新它。
那么为什么待处理的意图会停止触发呢?
提前致谢。
I have an app widget with a Button in it's layout.
When clicking the button, an intent is fired which calls my broadcast receiver.
It works just fine, but occasionally, after using the "Clear memory" button in the Task Manager, the widget gets stuck - clicking on it does nothing. But it can still receive updates from my app, if its running.
I'm not sure if the fact that the pending intent isn't fired is the memory clearing fault, or my fault.
Anyway, here's the code:
Registering the pending intent (onUpdate method of the app widget)
Intent intent = new Intent(context, ServiceControl.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
views.setOnClickPendingIntent(R.id.appwidgetbutton, pendingIntent);
and then updating the widgets with the views.
Here is the decleration of the app widget provider:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:initialLayout="@layout/appwidget"
android:updatePeriodMillis="0">
</appwidget-provider>
I don't want the system to call widget updates, I only update it from my app itself.
So why does the pending intent stop firing?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Jong
@CommonsWare
大家好,我想通了。 Ofc 这是 Android 问题,接收者应该始终接收。
现在如何解决它?显然所有的小部件都在工作,所以一定有一个简单的东西。
我在某处读到(试图找到那个人)提醒我们所有人,小部件类实际上是扩展了 BroadcastReceiver。
因此,您可以注册小部件(在清单中)以接收威胁本身。因此整个系统在AppWidgetProvider的类实例中是独立的。
现在,为了与应用程序进行通信,您可以在 onReceive 中调用应用程序的任何静态类,并且如果应用程序处于活动状态,LocalBroadcastManager 不会让您失败。如果它没有处于活动状态,那么您的按钮无论如何都应该开始活动!
如果你想要代码,我可以详细说明。
@Jong
@CommonsWare
Hi guys, I figured it out. Ofc this is an Android issue, the receiver should ALWAYS receive.
Now how to get around it? Obviously all the widgets are working, so there must have been a simple out there.
I read on SO somewhere (trying to find the guy) reminding us all that the widget class is actually extending a BroadcastReceiver.
So, you could register the widget (in the manifest) to receive the threats itself. Thus the entire system is self-contained in the class instance of AppWidgetProvider.
Now, for communicating back with the app, you can in the onReceive call any static class of your app, and LocalBroadcastManager won't fail you if the app is active. If it's not active, your buttons should be starting activities anyway!
Should you want the code, I can detail it.