到目前为止,我编码:
public class WidgetActivity extends AppWidgetProvider
{
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_activity);
Intent settingsIntent = new Intent(context, Info.class);
PendingIntent clickPendIntent = PendingIntent.getActivity
(context, 0, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.Widget, clickPendIntent);
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
}
}
}
没有成功我点击屏幕上的小部件并且没有任何内容启动。我错过了什么吗?
答案 0 :(得分:1)
使用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。