我创建了一个小部件,用于检索html原始数据并将其解析出来。数据正确地显示到窗口小部件,但是当我启动窗口小部件的配置活动并退出时,窗口小部件布局被重置,窗口小部件上没有显示任何内容,所有文本都被删除。小部件在android 4.0上完美运行并且没有这个问题,但在2.3.3上它有这种行为。关于问题是什么以及如何解决它的任何想法?
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
WidgetIds = appWidgetIds;
try {BUP.backupWidgetIdLength(context, WidgetIds.length);} catch (Exception e1) {e1.printStackTrace();}
for(int i = 0; i < WidgetIds.length; i++)
{
try {BUP.backupWidgetIds(context, WidgetIds[i], i);
} catch (Exception e) {e.printStackTrace();}
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
//updateWidget(context,appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
updateWidget(context,appWidgetId);
}
static void updateWidget(Context context,int appWidgetIds)
{
Intent intent = new Intent(context, MySchedule.class);
RemoteViews updateViews = new RemoteViews(context.getPackageName(),R.layout.main);
try
{
Intent sch = new Intent(context, FullSchedule.class);
sch.setAction(schedule_Click);
sch.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
sch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
updateViews.setOnClickPendingIntent(R.id.Rlayout4,PendingIntent.getActivity(context.getApplicationContext(),appWidgetIds,sch,PendingIntent.FLAG_UPDATE_CURRENT));
Intent f = new Intent(context, MyConfig.class);
f.setAction(Settings);
f.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
f.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
updateViews.setOnClickPendingIntent(R.id.Rlayout2,PendingIntent.getActivity(context.getApplicationContext(),appWidgetIds,f,PendingIntent.FLAG_UPDATE_CURRENT));
Intent s = new Intent(context, MySchedule.class);
s.setAction(leftSchedule_CLICK);
s.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pis = PendingIntent.getBroadcast(context.getApplicationContext(),appWidgetIds, s,PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setOnClickPendingIntent(R.id.Change_Schedule,pis);
Intent t = new Intent(context, MySchedule.class);
t.setAction(rightSchedule_CLICK);
t.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pit = PendingIntent.getBroadcast(context.getApplicationContext(),appWidgetIds, t,PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setOnClickPendingIntent(R.id.Change_Schedule2,pit);
Intent u = new Intent(context, MySchedule.class);
u.setAction(Update_Schedule);
u.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent piu = PendingIntent.getBroadcast(context.getApplicationContext(),appWidgetIds, u,PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setOnClickPendingIntent(R.id.updateView,piu);
AppWidgetManager appWidgetMan = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, MySchedule.class);
appWidgetMan.updateAppWidget(thisWidget, updateViews);
}catch(Exception e)
{
Log.d("Error_OnUpdate",e.getMessage());
}
}
}
`