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 随机停止工作


- UITextView 内容插图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- URL编码Swift iOS 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01