0

我做了一个application和一个widget。我必须widget从应用程序更新我的,即将一些数据发送到小部件。我使用sendBroadcast(Intent intent)了应用程序中的方法。它在onReceive(Context context, Intent intent)MyWidgetProvider 应用程序的方法中很受欢迎。我能够获取数据,但该onUpdate()方法似乎不起作用,因此小部件没有 UI。

这是小部件应用程序的代码

public class MyWidgetProvider extends AppWidgetProvider {

    public String team1name;
    public String team2name;
    public String team1score;
    public String team2score;    
    public static boolean done=false;

        public void onReceive(Context c, 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");    
            done=true;
        }

        private static final String ACTION_CLICK = "ACTION_CLICK";

          @Override
          public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {

            // Get all ids
            ComponentName thisWidget = new ComponentName(context,MyWidgetProvider.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
            for (int widgetId : allWidgetIds) {
                Log.e("WidgetExample", "The victory is mine");

              RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
              // Set the text
              remoteViews.setTextViewText(R.id.update, team1name);

              // Register an onClickListener
              Intent i = new Intent(context, MyWidgetProvider.class);

              i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
              i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

              PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, i, PendingIntent.FLAG_UPDATE_CURRENT);
              remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
              appWidgetManager.updateAppWidget(widgetId, remoteViews);
            }
          }
4

2 回答 2

0

I had to make use of RemoteViews in the onReceive method itself. This worked for me.

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");
    player1 = extras.getString("player1");
    player2 = extras.getString("player2");
    extras = null;

    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.teamscore, team2score);  
    remoteViews.setTextViewText(R.id.batsman1, player1);
    remoteViews.setTextViewText(R.id.bowler, player2);

    ComponentName cn = new ComponentName(context, MyWidgetProvider.class);
    AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
}
于 2012-09-16T07:22:07.650 回答
0
public void onReceive(Context c, 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");    
    done = true;
}

您应该onUpdateonReceive. 接收广播不仅会自行AppWidgetProvider调用 onUpdate。您需要手动执行此操作。

于 2012-09-09T07:51:15.263 回答