问题描述
我需要知道的小部件ID内的onReceive()。我以为到了配置活动为新的小部件ID的选择项目信息相关联,然后将其保存到共享preferences,这样我可以知道该怎么办里面onReiceive()通过共享preferences阅读
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);
}
BroadcastReceiver的:
BroadCastReceiver:
public void onReceive(Context context, Intent intent)
{
intent.getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID); // <-- NULL
..
}
getStringExtra总是返回空值......也许是code以上是完全错误的。
getStringExtra returns always null values... maybe the code above is completely wrong
推荐答案
有几件事情...
-
onEnabled
在AppWidgetProvider只有当第一appwidget加入调用一次(在此AppWidgetProvider变为已启用这是)。请注意,onEnabled不给你的appWidgetId
- 这是不是与主屏幕上的应用程序窗口小部件的特定实例相关联的回调 - 您正在呼叫
getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)
在意向刚刚创建。我想你的意思是叫putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId)
。然而,正如上面提到的,这也不行,因为你没有给出一个ID在onEnabled()
。
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
共享preferences(你可以使用键appwidgetid _ ##来存储一个布尔值,例如)。 - 当
的OnUpdate()
被调用,你遍历appWidgetIds
数组,检查每个appWidgetId
共享preferences第一。如果检查通过,你知道用户已经配置了appWidget,你可以创建和设置你的闹钟吧;否则,继续
下appWidgetId
。 - 当设置闹钟,请注意,
意图
创建PendingIntent
S当s必须是唯一的,否则你会获得PendingIntent
一个可重用或覆盖旧的(根据您指定作为最后一个参数到标志上的PendingIntent
呼叫)。由于额外检查的独特性时,不考虑,看到code底部的如何使它独一无二的。 - 在
onDelete()
,取消报警的appwidget。请确保您构建PendingIntent完全相同的方式。您还可以在这里删除appWidgetId
从共享preferences。
- 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部件编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!