Get widget id from BroadcastReceiver(从 BroadcastReceiver 获取小部件 id)
问题描述
我需要知道 onReceive() 中的小部件 ID.我想将配置活动的选定项目信息与新的小部件 id 相关联,然后将它们保存到 sharedpreferences 以便我可以通过读取 sharedpreferences 来知道在 onReiceive() 内部做什么
I need to know the widget id inside onReceive(). I thought to associate the selected item informations of the configure activity to the new widget id, and then save them to sharedpreferences so that i can know what to do inside onReiceive() by reading from sharedpreferences
配置活动:
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();
}
});
AppWidgetProvider:
AppWidgetProvider:
@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 总是返回空值...也许上面的代码完全错误
getStringExtra returns always null values... maybe the code above is completely wrong
推荐答案
一些事情...
- AppWidgetProvider 中的
onEnabled仅在添加第一个 appwidget 时调用一次(即当此 AppWidgetProvider 变为启用"时).请注意,onEnabled 不会为您提供appWidgetId- 它不是与主屏幕上应用小部件的特定实例相关联的回调.- 您正在对刚刚创建的 Intent 调用
getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID).我认为您的意思是调用putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId).但是,如上所述,这也不起作用,因为您没有在onEnabled()中获得 id.
onEnabledin an AppWidgetProvider is only called once when the first appwidget is added (that's when this AppWidgetProvider becomes "enabled"). Notice that onEnabled doesn't give you anappWidgetId- it's not a callback associated with a particular instance of an app widget on the home screen.- You are calling
getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)on an Intent you've just created. I think you meant to callputExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId). However, as mentioned above, this won't work either since you aren't given an id inonEnabled().
如果要在主屏幕上设置与每个 appwidget 实例相关联的警报,则需要做一些额外的工作.
If you want to set an alarm that is associated to each appwidget instance on the home screen, you need to do some extra work.
- 完成配置应用小部件后,将其
appWidgetId存储在 SharedPreferences 中(例如,您可以使用键appwidgetid_##"来存储布尔值). - 当
onUpdate()被调用并且您正在迭代appWidgetIds数组时,首先检查 SharedPreferences 中的每个appWidgetId.如果检查通过,您就知道用户已经配置了该 appWidget,您可以为其创建和设置闹钟;否则,continue到下一个appWidgetId. - 设置闹钟的时候,注意
Intent在创建PendingIntent的时候必须是唯一的,否则你会得到一个PendingIntent会复用或覆盖旧的(取决于您指定为PendingIntent调用的最后一个参数的标志).由于在检查唯一性时不考虑额外内容,请参阅底部的代码了解如何使其唯一性. - 在
onDelete()中,取消该 appwidget 的警报.确保以完全相同的方式构造 PendingIntent.您还可以在此处从 SharedPreferences 中删除appWidgetId.
- When you finish configuring an app widget, store its
appWidgetIdin SharedPreferences (you could use the key "appwidgetid_##" to store a boolean value, for instance). - When
onUpdate()is called and you are iterating over theappWidgetIdsarray, check eachappWidgetIdin SharedPreferences first. If the check passes, you know the user has configured that appWidget and you can create and set your alarm for it; otherwise,continueto the nextappWidgetId. - When setting the alarm, note that
Intents must be unique when creatingPendingIntents, otherwise you'll get aPendingIntentthat reuses or overwrites the old one (depending on which flag you specify as the last argument to thePendingIntentcall). Since extras are not considered when checking for uniqueness, see the code at the bottom for how to make it unique. - In
onDelete(), cancel the alarm for that appwidget. Make sure you construct the PendingIntent the exact same way. You can also remove theappWidgetIdfrom SharedPreferences here.
使意图独一无二:
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWIdgetId);
// IMPORTANT!
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Note the FLAG_UPDATE_CURRENT
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = ...
am.setInexactRepeating(...);
这篇关于从 BroadcastReceiver 获取小部件 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 BroadcastReceiver 获取小部件 id
基础教程推荐
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
