当我单击我的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);
}
}