我正在开发一个小部件,它运行时没有任何错误,但ImageView
我的小部件没有更新,也没有给出任何错误。因此让我对自己做错了什么感到困惑。这是代码: -
BroadcastReceiver _broadcastReceiver;
GetDrawableResources getDrawableResources = new GetDrawableResources();
private final SimpleDateFormat _sdfWatchTime = new SimpleDateFormat("HH:mm");
@Override
public void onEnabled(final Context context) {
super.onEnabled(context);
// TODO Auto-generated method stub
System.out.println("Entering onstart");
_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
System.out.println("Registering BroadCast");
if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
System.out.println(_sdfWatchTime.format(new Date()));
Bundle extras = intent.getExtras();
if (extras != null) {
AppWidgetManager awm = AppWidgetManager
.getInstance(context);
ComponentName thisWidget = new ComponentName(
context.getPackageName(),
MainActivity.class.getName());
int[] appWidgetIds = awm.getAppWidgetIds(thisWidget);
onUpdate(context, awm, appWidgetIds);
}
}
}
};
context.getApplicationContext().registerReceiver(_broadcastReceiver,
new IntentFilter(Intent.ACTION_TIME_TICK));
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int awID : appWidgetIds) {
updateAppWidget(context, appWidgetManager, awID);
}
}
private void updateAppWidget(Context context,
AppWidgetManager appWidgetManager, int awID2) {
// TODO Auto-generated method stub
// class to get drawable images that will be used to denote hours and
// minutes
int time[] = new int[new GetTime().getTime(context
.getApplicationContext()).length];
// class to get each digit of time separately eg 10:10, separating 1 0 1
// 0 working correctly
time = new GetTime().getTime(context.getApplicationContext());
// changing views
System.out.println("awID for " + awID2);
RemoteViews views = new RemoteViews(context
.getPackageName(), R.layout.activity_main);
views.setImageViewResource(R.id.ivHour0,
getDrawableResources.setTimeDrawable(time[0]));
views.setImageViewResource(R.id.ivHour1,
getDrawableResources.setTimeDrawable(time[1]));
views.setImageViewResource(R.id.ivMinute0,
getDrawableResources.setTimeDrawable(time[2]));
views.setImageViewResource(R.id.ivMinute1,
getDrawableResources.setTimeDrawable(time[3]));
System.out.println("updating the views");
appWidgetManager.updateAppWidget(awID2, views);
}
我的 LogCat 中的输出
06-28 19:46:00.076: I/System.out(680): Registering BroadCast
06-28 19:46:00.086: I/System.out(680): 19:46
06-28 19:46:00.096: I/System.out(680): awID for 14
06-28 19:46:00.096: I/System.out(680): updating the views
06-28 19:47:00.086: I/System.out(680): Registering BroadCast
06-28 19:47:00.086: I/System.out(680): 19:47
06-28 19:47:00.096: I/System.out(680): awID for 14
06-28 19:47:00.096: I/System.out(680): updating the views
因此,appWidgetID 是正确的,并且函数正在执行,但视图没有更新。它们和以前一样。