1

到目前为止,我已经编码:

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);


        }
    }
}

没有成功,我在屏幕上单击小部件并且没有任何启动。我错过了什么吗?

4

1 回答 1

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在首次将小部件添加到主屏幕时自动弹出信息,您应该阅读这条有用的信息

于 2012-10-18T00:26:00.733 回答