简体   繁体   English

如何从另一个应用程序更新小部件?

[英]How to update widget from another application?

I have made an application and a widget . 我已经制作了一个application和一个widget I have to update my widget from the application ie send some data to the widget. 我必须从应用程序更新我的widget ,即向该窗口小部件发送一些数据。 I used the sendBroadcast(Intent intent) method from the application. 我从应用程序中使用了sendBroadcast(Intent intent)方法。 It is well received in the onReceive(Context context, Intent intent) method of MyWidgetProvider app. 在MyWidgetProvider应用程序的onReceive(Context context, Intent intent)方法中可以很好地接收到它。 I am able to get the data, But the onUpdate() method does not seem to work and hence no UI for the widget. 我能够获取数据,但是onUpdate()方法似乎不起作用,因此没有小部件的UI。

this is the code for widget app 这是小部件应用程序的代码

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

You are supposed to call onUpdate from onReceive . 您应该从onReceive调用onUpdate Receiving the broadcast won't just make the AppWidgetProvider call onUpdate by itself. 接收广播不仅会使AppWidgetProvider自己AppWidgetProvider调用onUpdate。 You need to do that manually. 您需要手动执行此操作。

I had to make use of RemoteViews in the onReceive method itself. 我必须在onReceive方法本身中使用RemoteViews。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM