我创建了一个小部件,可以使用存储在SharedPreferences中的不同设置进行多次实例化。在配置活动(Multiple, configurable widgets - update issue)之后直接解决了非更新小部件的初始问题后,一切似乎都正常。但。当主屏幕应用程序被杀/重启(我的情况下是Trebuchet,Android 4.3和4.2.2)小部件未刷新且onClick侦听器消失(onUpdate操作未被触发)。我只看到xml默认的非更新布局的无响应空WigdetView。更奇怪的是 - 手机重启后或添加我的小部件的另一个实例后 - 一切正常。
我已经尝试了很多不同的方法来解决这个奇怪的问题(比如单一引用RemoteViews,使用Service而不是AsyncTask等) - 但这些想法都没有解决问题..
代码中有很多重要的逻辑,快捷方式:
的AppWidgetProvider :
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// ASYNCTASK WAY
new DownloadTask(context, appWidgetManager).execute("http://sampleurl/");
/*
// OR SERVICE WAY
// SERVICE LOGIC IS ALMOST THE SAME AS IN DownloadTask
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.startService(intent);
*/
}
@Override
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
int mAppWidgetId=0;;
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
String action = intent.getAction();
if (action.equals("PreferencesUpdated")) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.menuLayout, View.GONE);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
int[] appWidgetIds = new int[] {mAppWidgetId};
onUpdate(context, appWidgetManager, appWidgetIds);
}
else if (action.equals("ShowMenu")) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.menuLayout, View.VISIBLE);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
}
super.onReceive(context, intent);
}
DownloadTask
public DownloadTask(Context context, AppWidgetManager appWidgetManager) {
this.context = context;
this.appWidgetManager = appWidgetManager;
}
@Override
protected String doInBackground(String... url) {
// 1. DOWNLOAD FILE
// 2. PROCESS FILE, ADD TO DATABASE, ETC
// 3. UPDATE ALL WIDGETS
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
int myPref= settings.getInt("myPref"+widgetId, -1);
if(myPref!= -1){
Log.d("DownloadTask", "widget id:"+widgetId);
updateWidget(db, widgetId, selectedStation);
}
}
db.close();
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//SHOW PROGRESSBAR (LOADER) ON ALL WIDGETS
//ADD onClicks
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
views.setViewVisibility(R.id.progressLayout, View.VISIBLE);
views.setOnClickPendingIntent(R.id.linearLayout1, getMenuPendingIntent(context, widgetId));
views.setOnClickPendingIntent(R.id.refreshButton, getRefreshPendingIntent(context, widgetId));
views.setOnClickPendingIntent(R.id.settingsButton, getSettingsPendingIntent(context, widgetId));
appWidgetManager.updateAppWidget(widgetId, views);
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//HIDE PROGRESSBAR (LOADER) ON ALL WIDGETS
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.progressLayout, View.INVISIBLE);
appWidgetManager.updateAppWidget(thisWidget, views);
if (exception){
Toast.makeText(context, context.getString(R.string.net_exc), Toast.LENGTH_LONG).show();
}
}
public void updateWidget(MySQLiteHelper db, int widgetId, int myPref){
// UPDATE SINGLE WIDGET
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setTextViewText(R.id.someText, "someText");
// ETC
appWidgetManager.updateAppWidget(widgetId, views);
}
我真的希望你们可以帮助我,因为我被困住了。
PS。这是所有小部件一起刷新的预期行为。
答案 0 :(得分:1)
遇到类似问题后,我的小部件在重启启动器/主屏幕后无法正常工作,这里有一个可能的解决方案:
.setOnClickPendingIntent(...)
。基本上,在您修改onReceive()
的{{1}}中添加相应的行以添加RemoteViews
,以及您可能修改这些行的其他任何地方。
<强> PS:强>
您可以删除for循环迭代所有widgetIds并只使用
PendingIntent
一次更新所有观看次数。