0

当我单击我的widget. 它似乎不起作用。请告诉我哪里出错了。到目前为止,这是我的代码。

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

1 回答 1

0

我只是将一些代码从 onUpdate() 方法添加到 onReceive() 方法,它就起作用了。这是代码。

      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);
于 2012-09-16T12:36:37.680 回答