private void RefreshWidget(DataSourceObject result) {
Intent intent = new Intent(context, activity_main.class); String str = result != null ? String.valueOf(result.getid()) : "0"; intent.putExtra("xmlKeyByWidgetProvider", str); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); RemoteViews myRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); myRemoteViews.setOnClickPendingIntent(R.id.widgetLayout, pendingIntent); myRemoteViews.setTextViewText(R.id.widgetEnText, result.getdatasource_En()); myRemoteViews.setTextViewText(R.id.widgetCnText, result.getdatasource_Cn()); ComponentName mComponentName = new ComponentName(context, WidgetProvider.class); AppWidgetManager myAppWidgetManager = AppWidgetManager .getInstance(context); myAppWidgetManager.updateAppWidget(mComponentName, myRemoteViews); }
这是我在BroadcastReceiver里面刷新AppWidgetManager的代码,在Activity里面我写了这样的代码用于获取值。
@Override
protected void onResume() { super.onResume(); Intent intent = this.getIntent(); if (intent == null) return; if (intent.hasExtra("xmlKeyByWidgetProvider")) { String str = intent.getStringExtra("xmlKeyByWidgetProvider"); Log.i("ssss", str); gallery.setSelection(Integer.parseInt(str)); }
}
但是Intent并没有获取到AppWidgetManager传过来的值。