我的布局中有一个imageview,我想用appwidgetprovider活动每隔x秒更改一次。但每次我使用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);
}
}
答案 0 :(得分:0)
您只能在findViewById
中使用Activity
。您可以在ImageView
中获得Activity
个实例,并将其作为参数传递给Widget
。