我需要知道 onReceive() 中的小部件 ID。我想将配置活动的选定项目信息与新的小部件 id 相关联,然后将它们保存到 sharedpreferences 以便我可以通过读取 sharedpreferences 来知道在 onReiceive() 内部做什么
配置活动:
resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetId);
setResult(RESULT_CANCELED, resultValue);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
...
resultValue.putExtra("mykey", "otherinfo");
setResult(RESULT_OK, resultValue);
finish();
}
});
应用小部件提供者:
@Override
public void onEnabled(Context context)
{
super.onEnabled(context);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
int id = intent.getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID) // <-- THIS IS NULL!
// save id on shared preferences
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), UPDATE_INTERVAL, pi);
}
广播接收器:
public void onReceive(Context context, Intent intent)
{
intent.getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID); // <-- NULL
..
}
getStringExtra 总是返回空值......也许上面的代码是完全错误的