我的布局中有一个 imageview,我希望它每 x 秒通过 appwidgetprovider 活动更改一次。但是每次我使用 findViewById 时,它都会给我一个错误,即 findViewById(int) 方法对于 Widget 类型是未定义的。这是我的代码
public class Widget extends AppWidgetProvider {
int []imageArray={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3};
private Handler mHandler = new Handler();
RemoteViews views;
AppWidgetManager appWidgetManager;
ComponentName currentWidget;
Context context;
TextView textview;
DateFormat format = new SimpleDateFormat("HH:mm");
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
this.context = context;
this.appWidgetManager = appWidgetManager;
views = new RemoteViews(context.getPackageName(), R.layout.widget);
currentWidget = new ComponentName(context, Widget.class);
mHandler.removeCallbacks(mUpdateTask);
mHandler.postDelayed(mUpdateTask, 100);
}
final Runnable mUpdateTask = new Runnable() {
public void run() {
Intent informationIntent = new Intent(context, MainActivity.class);
PendingIntent infoPendingIntent = PendingIntent.getActivity(
context, 0, informationIntent, 0);
views.setOnClickPendingIntent(R.id.Widget, infoPendingIntent);
views.setTextViewText(R.id.widget_textview,
"" + format.format(new Date()));
appWidgetManager.updateAppWidget(currentWidget, views);
mHandler.postDelayed(mUpdateTask, 1000);
}
};
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
mHandler.removeCallbacks(mUpdateTask);
}
}