Android: How to update widget text on creation

前端 未结 1 669
悲哀的现实
悲哀的现实 2021-01-22 03:12

I have seen several questions regarding how to update widgets but nothing is helping me with my issue.

I created a widget that has a textview that I want to dynamically

1条回答
  •  [愿得一人]
    2021-01-22 03:33

    @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    
            //set widget id
            ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    
            for (int i = 0; i < appWidgetIds.length; i++) {
                Intent intentService = new Intent(context, WidgetService.class);
    
                intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
                intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));
    
                RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
    
                Intent clickIntent = new Intent(context, MainActivity.class);
                PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
                widget.setTextViewText(R.id.currency, " Dollar");
    
                appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
            }
            super.onUpdate(context, appWidgetManager, appWidgetIds);
    
        }
    

    This code works for me. You may try this.

    0 讨论(0)
提交回复
热议问题