我正在创建一个用于学习目的的模拟时钟小部件,并想知道如何进行设置布局以更改时钟刻度盘图像。我已经完成了小部件,但我似乎无法获得一个onclicklstener来调出设置布局。
这就是我所拥有的:
@Override
public void onDeleted(Context context, int[] appWidgetIds)
{super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.alarmclock", "com.android.alarmclock.AlarmClock"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
views.setOnClickPendingIntent(R.id.Widget, pendingIntent);
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.settings);
Intent launchActivity = new Intent(context,Widget.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0, launchActivity, 0);
updateViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);
ComponentName myComponentName = new ComponentName(context, Settings.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myComponentName, updateViews);
}
}
答案 0 :(得分:0)
此问题再次被问到here。
对于未来的访客:
在PendingIntent
方法中放置确保设置onUpdate()
的代码。这样可以确保只要将小部件放在主屏幕上,就会设置PendingIntent
。
所以:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgets) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_activity);
Intent intent = new Intent(context, Info.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);
此外:
如果您还希望在首次将小部件添加到主屏幕时自动弹出信息Activity
,则应阅读this userful piece of information。