我的app小部件需要帮助。我需要从任何活动中强制更新他。小部件反映来自应用程序的列表,并在我对列表进行一些更新或删除之后。我需要在小部件中显示真实数据。请帮助,我如何从任何应用程序活动强制更新此小部件?
AppWidgetProvider
public class Widget extends AppWidgetProvider {
...
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(TOAST_ACTION)) {
int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
String id = intent.getStringExtra(EXTRA_ITEM);
Intent uIntent = new Intent(context, UlohaShowerActivity.class);
uIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
uIntent.putExtra(UlohaShowerActivity.ODOSLI, id);
context.startActivity(uIntent);
}
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_uloha_layout);
Intent intent = new Intent(context, UlohaPridajActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
remoteViews.setOnClickPendingIntent(R.id.widgetUlohaAdd, pendingIntent);
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.listUloha_w, svcIntent);
remoteViews.setEmptyView(R.id.listUloha_w, R.id.empty_view);
Intent toastIntent = new Intent(context, Widget.class);
toastIntent.setAction(Widget.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.listUloha_w, toastPendingIntent);
return remoteViews;
}
}
RemoteViewsFactory
public class ListProvider implements RemoteViewsFactory {
....
public ListProvider(Context context, Intent intent) {
this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
populateListItem();
}
...
@Override
public RemoteViews getViewAt(int position) {
final RemoteViews remoteView = new RemoteViews(
context.getPackageName(), R.layout.uloha_list_row);
ListItem listItem = listItemList.get(position);
remoteView.setTextViewText(R.id.nazov, listItem.nazov);
remoteView.setTextViewText(R.id.datum, listItem.datum);
remoteView.setTextViewText(R.id.cas, listItem.cas);
Bundle extras = new Bundle();
extras.putString(Widget.EXTRA_ITEM, listItem.id);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
remoteView.setOnClickFillInIntent(R.id.uloha_list_row, fillInIntent);
try {
System.out.println("Loading view " + position);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return remoteView;
}
...
}
WidgetService
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
return (new ListProvider(this.getApplicationContext(), intent));
}
}
答案 0 :(得分:0)
我通过创建appWidgetProvider的静态方法来更新窗口小部件视图,找到了解决simmilar问题的方法。系统可以从原始onUpdate方法调用此方法,也可以随时调用此方法:
private static int[] savedAppWidgetIds;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
savedAppWidgedIds = appWidgetIds;
MyAppWidgetProvider.onUpdateMyView(context);
}
public static void onUpdateMyView(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_uloha_layout);
for (int i = 0; i < savedAppWidgetIds.size; i++) {
int appWidgetId = savedAppWidgetIds[i];
// update your widget,
然后你可以通过以下方式从任何地方调用onUpdateMyWidget()方法:
MyAppWidgetProvider.onUpdateMyView(context);