The method startActivity(Intent) is undefined for the type?

前端 未结 2 724
北荒
北荒 2021-01-16 17:18

So im trying to start an activity by pressing a widget. I keep running into the error \"The method startActivity(Intent) is undefined for the type Photos\" any help is much

2条回答
  •  醉话见心
    2021-01-16 17:38

    You need to implement the onUpdate() and onReceive() methods in your AppWidgetProvider class, something like this:

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
        int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        for (int i=0; i

    Then write your onReceive() method to receive the pending intent and start the relevant activity:

    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    
        if (intent.getAction().equals("PhotosAction") {
            //Received photos action action, start your target activity
            Intent i = new Intent(android.provider.Settings.ACTION_SETTINGS);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
    

提交回复
热议问题