Подтвердить что ты не робот

Программно обновлять виджет из активности/службы/получателя

Я знаю, что это возможно, но я не могу понять, как инициировать обновление моего виджета из основного действия. Разве нет каких-то общих намерений, которые я могу транслировать?

4b9b3361

Ответ 1

Если вы используете AppWidgetProvider, вы можете обновить его следующим образом:

Intent intent = new Intent(this, MyAppWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
 int[] ids = AppWidgetManager.getInstance(getApplication())
    .getAppWidgetI‌​ds(new ComponentName(getApplication(), MyAppWidgetProvider.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

Ответ 2

Это помогло, когда я просмотрел , как обновить виджет из службы/или действия (но может быть возможным из каждого контекста):

Context context = this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_2x1);
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
remoteViews.setTextViewText(R.id.my_text_view, "myText" + System.currentTimeMillis());
appWidgetManager.updateAppWidget(thisWidget, remoteViews);

Ответ 3

Итак, чтобы обновить виджет от активности, вы можете сделать это следующим образом:

Intent intent = new Intent(this, DppWidget.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), DppWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);

Работает для меня:)

Ответ 4

Попробуйте следующее:

int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), MyWidget.class));
        MyWidget myWidget = new MyWidget();
        myWidget.onUpdate(this, AppWidgetManager.getInstance(this),ids);

Ответ 5

int widgetIDs[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), WidgetProvider.class));

for (int id : widgetIDs)
    AppWidgetManager.getInstance(getApplication()).notifyAppWidgetViewDataChanged(id, R.id.widget_view);
}

Ответ 6

Если вы работаете с виджетами, которые используют коллекцию, такую ​​как ListView, GridView или StackView, для обновления элементов виджетов, выполните следующие действия:

Context context = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, StackWidgetProvider.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view);

С помощью этого метода notifyAppWidgetViewDataChanged() вы можете заставить элементы виджета получать новые данные в реальном времени.

Ответ 7

Просто получите экземпляр AppWidgetManager и обновите его напрямую. Или, если вы используете IntentService для выполнения обновлений, вызовите startService() на IntentService. AppWidgetProvider - это a способ обновления виджета приложения, но это не единственный способ.