How to get to another application on click of a wi

2019-09-17 12:24发布

I want to get to another application when I click my widget. It doesn't seem to be working. Please tell me where I am going wrong. Here's my code so far.

public class MyWidgetProvider extends AppWidgetProvider {

    public String team1name;
    public String team2name;
    public String team1score;
    public String team2score;
          @Override
            public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
                super.onUpdate(context, appWidgetManager, appWidgetIds);
                Log.i(MyWidgetProvider.class.toString(), "Onupdate called for " + this);

                  RemoteViews remoteViews = new RemoteViews(context.getPackageName (), R.layout.widget_layout); 

                  Intent in = new Intent(Intent.ACTION_MAIN);
                  PackageManager packageManager = context.getPackageManager();
                  in = packageManager.getLaunchIntentForPackage("com.playup.android.adapters");
                  in.addCategory(Intent.CATEGORY_LAUNCHER);
                  PendingIntent pendingIntent2 = PendingIntent.getActivity(context,0 /* no requestCode */, in, 0 /* no flags */);
                  remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent2);

                  ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
                  AppWidgetManager manager = AppWidgetManager.getInstance(context);
                  manager.updateAppWidget(thisWidget, remoteViews);
            }
            public void onReceive(Context context, Intent intent){
                Bundle extras = intent.getExtras();
                if (extras == null) {
                  return;
                }
                team1name = extras.getString("team1name");
                team2name = extras.getString("team2name");
                team1score = extras.getString("team1score");
                team2score = extras.getString("team2score");    


                    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                      remoteViews.setTextViewText(R.id.team1name, team1name);
                      remoteViews.setTextViewText(R.id.team2name, team2name);
                      remoteViews.setTextViewText(R.id.team1score, team1score);
                      remoteViews.setTextViewText(R.id.team2score, team2score);

                        ComponentName cn = new ComponentName(context, MyWidgetProvider.class);  
                        AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
            }
} 

1条回答
戒情不戒烟
2楼-- · 2019-09-17 13:24

I simply added some code from onUpdate() method to onReceive() method and it worked. This is the code.

      Intent in = new Intent(Intent.ACTION_MAIN);
      PackageManager packageManager = context.getPackageManager();
      in = packageManager.getLaunchIntentForPackage("com.playup.android.adapters");
      in.addCategory(Intent.CATEGORY_LAUNCHER);
      PendingIntent pendingIntent2 = PendingIntent.getActivity(context,0 /* no requestCode */, in, 0 /* no flags */);
      remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent2);
查看更多
登录 后发表回答