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.
onEnabled
in 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
appWidgetId
in SharedPreferences (you could use the key "appwidgetid_##" to store a boolean value, for instance). - When
onUpdate()
is called and you are iterating over theappWidgetIds
array, check eachappWidgetId
in 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,continue
to the nextappWidgetId
. - When setting the alarm, note that
Intent
s must be unique when creatingPendingIntent
s, otherwise you'll get aPendingIntent
that reuses or overwrites the old one (depending on which flag you specify as the last argument to thePendingIntent
call). 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 theappWidgetId
from 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
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01