Settings Activity Not Launching

断了今生、忘了曾经 提交于 2019-12-02 08:01:43

问题


So far i have coded:

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


        }
    }
}

To no success i click the widget when its on screen and nothing launches. Am I missing anything?


回答1:


Put the code that makes sure the PendingIntent is set, in the onUpdate() method instead. This makes sure that as soon as the widget is put on the homescreen that the PendingIntent is set.

So:

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

Also:

If you would also like to have the info Activity pop up automatically when first adding the widget to the home screen, you should read this userful piece of information.



来源:https://stackoverflow.com/questions/12945402/settings-activity-not-launching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!