Launching activity from widget

后端 未结 9 1185
深忆病人
深忆病人 2020-11-29 00:26

I\'m trying to do something which really ought to be quite easy, but it\'s driving me crazy. I\'m trying to launch an activity when a home screen widget is pressed, such as

9条回答
  •  余生分开走
    2020-11-29 00:52

    This worked for me, based on info here, the word widget sample, and the tutorial here

           Intent intent = new Intent(Intent.ACTION_MAIN, null);
          intent.addCategory(Intent.CATEGORY_LAUNCHER);
          // first param is app package name, second is package.class of the main activity
          ComponentName cn = new ComponentName("com....","com...MainActivity");
          intent.setComponent(cn);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          PendingIntent myPI = PendingIntent.getActivity(context, 0, intent, 0); 
    
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_word); 
    
    
        views.setOnClickPendingIntent(R.id.widget, myPI); 
    
        AppWidgetManager mgr = AppWidgetManager.getInstance(context); 
        mgr.updateAppWidget(comp, views); 
    

提交回复
热议问题