我有一个小部件,只要有配置更改(例如屏幕方向)以及手机解锁,它就会自行更新。此过程涉及onClick
为我的小部件上的按钮设置处理程序。这很好用,但是我发现有一个用例导致我的应用程序不响应onClick
事件。这种特殊情况是每当启动器自行重新启动时。
有没有办法检测启动器何时重新启动,以便我可以手动更新我的小部件?还是有另一种方法来确保onClick
处理程序不会丢失?
我有一个小部件,只要有配置更改(例如屏幕方向)以及手机解锁,它就会自行更新。此过程涉及onClick
为我的小部件上的按钮设置处理程序。这很好用,但是我发现有一个用例导致我的应用程序不响应onClick
事件。这种特殊情况是每当启动器自行重新启动时。
有没有办法检测启动器何时重新启动,以便我可以手动更新我的小部件?还是有另一种方法来确保onClick
处理程序不会丢失?
事实证明,new RemoteViews()
当我应该调用一次以生成视图时,我正在发送垃圾邮件,然后在需要时引用该实例。在我的解决方案中,我有一个存储这个 RemoteView 实例的类变量和一个访问它的 getter。
@Glitch 的提议可能不适用于某些情况,尤其是带有ListView
. 这是因为在多次调用之后ListView
会变得很慢(尝试滚动ListView
) 。appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id)
我的猜测是,单个RemoteView
实例会将其所有执行的指令保存在一个列表中。随着时间的推移,指令列表将会增长。每次appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id)
,都会重新执行大指令列表。
我提出的解决方案如下。但是,我相信它只适用于某些设备,因为并非所有设备在启动器重新启动期间都会收到相同的广播消息。
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals("com.sec.android.widgetapp.APPWIDGET_RESIZE")) {
// http://stackoverflow.com/questions/17396045/how-to-catch-widget-size-changes-on-devices-where-onappwidgetoptionschanged-not
handleTouchWiz(context, intent);
// Possible launcher restart.
handleLauncherRestart(context, intent);
} else if (action.equals("android.appwidget.action.APPWIDGET_UPDATE_OPTIONS")) {
// Possible launcher restart.
handleLauncherRestart(context, intent);
}
super.onReceive(context, intent);
}
private void handleLauncherRestart(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
updateAppWidget(context, appWidgetManager, appWidgetId);
}
private void handleTouchWiz(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetId = intent.getIntExtra("widgetId", 0);
int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
int widgetSpanY = intent.getIntExtra("widgetspany", 0);
if (appWidgetId > 0 && widgetSpanX > 0 && widgetSpanY > 0) {
Bundle newOptions = new Bundle();
// We have to convert these numbers for future use
// http://stackoverflow.com/questions/10008521/appwidget-size-calculation
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 74 - 2);
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 74 - 2);
} else {
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 70 - 30);
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 70 - 30);
}
onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}
}