我现在不介绍如何将Appwidget用于自定义时钟。 它始终在移动桌面上显示“问题加载小部件”。
公共类ClockWidget扩展了AppWidgetProvider { 静态最终字符串CLICK_ACTION =“ com.example.tina.action_CLICK”;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.activity_main);
Intent intent = new Intent(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, R.id.clockview, intent, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.clockview, pendingIntent);
for (int appWidgetId : appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId, view);
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (CLICK_ACTION.equals(intent.getAction())) {
Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
@Override
public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
super.onRestored(context, oldWidgetIds, newWidgetIds);
}
}
<com.example.tina.ondraw_examle_clock.ClockView
android:id="@+id/clockview"
android:layout_width="200dp"
android:layout_height="200dp" />