Android Widget stops working randomly(Android Widget 随机停止工作)
问题描述
我已经解决这个问题三天了,我已经查看了这里的每一个问题以寻求答案.我有一个带有按钮的小部件,我想做的就是每次单击它时启动一个服务.问题是按钮随机停止工作.我根本无法重新创建它,我不知道是什么原因造成的.我的服务调用 stopSelf();但是我的广播接收器也遇到了这个问题,所以我相信问题出在小部件上,而不是服务上.这是代码
I have been working with this problem for three days now and I've looked at every single question on here for an answer. I have a widget with a button on it and all I would like it to do is start a service everytime it is clicked. The problem is that the button stops working randomly. I can't recreate it at all and I have no idea what causes it. My service calls stopSelf(); but I have had this problem with a broadcast receiver also so I believe the problem to be in the widget and not in the service. Here is the code
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Log.i("Widget", "onUpdate");
final int N = appWidgetIds.length;
for (int i=0; i<N; i++)
{
int appWidgetId = appWidgetIds[i];
Log.i("Widget", "onUpdateLoop");
Intent intent = new Intent(context, ServiceWidgetAction.class);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent
.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget);
views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);
views.setTextViewText(
R.id.widgetTextView,
"Some text");
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
我一遍又一遍地检查了这段代码,但我不知道它为什么会这样.它在 3 部不同的手机上执行此操作,我无法重新创建它.我试过强制停止应用程序,从任务管理器中清除它,添加新的小部件等,以使小部件停止工作,但它不会.但是,当我今天早上醒来时,小部件不再起作用.任何帮助将不胜感激.
I've checked this code over and over and I have no idea why it is doing this. It does this on 3 separate phones and I can't recreate it. I've tried force stopping the app, clearing it from a task manager adding new widgets etc. to get the widget to stop working and it won't. However when I woke up this morning the widget no longer works. Any help would be GREATLY appreciated.
推荐答案
我最终找出了问题所在.我正在从应用程序的其他地方更新小部件,但我没有每个地方的完整代码.换句话说,我的应用程序像这样更新了小部件中的 textviews
I ended up figuring out the problem. I was updating the widget from other places in the app and I did not have the complete code for each one. In other words my app updated the textviews in the widget like this
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget);
views.setTextViewText(
R.id.widgetTextView,
"Some text");
appWidgetManager.updateAppWidget(appWidgetId, views);
但它需要整个代码来保持按钮注册如下:
But it needed the entire code to keep the button registered like this:
Intent intent = new Intent(context, ServiceWidgetAction.class);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent
.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget);
views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);
views.setTextViewText(
R.id.widgetTextView,
"Some text");
appWidgetManager.updateAppWidget(appWidgetId, views);
因此,最重要的是,每当您从应用程序中更新小部件时,请确保它看起来与小部件本身中的代码完全相同.不要遗漏任何东西.
So bottom line is whenever you update a widget from within your app, make sure it looks exactly like the code in the widget itself. Don't leave anything out.
这篇关于Android Widget 随机停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android Widget 随机停止工作
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01