从 BroadcastReceiver 获取小部件 id

Get widget id from BroadcastReceiver(从 BroadcastReceiver 获取小部件 id)

本文介绍了从 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 中的
  1. onEnabled 仅在添加第一个 appwidget 时调用一次(即当此 AppWidgetProvider 变为启用"时).请注意,onEnabled 不会为您提供 appWidgetId - 它不是与主屏幕上应用小部件的特定实例相关联的回调.
  2. 您正在对刚刚创建的 Intent 调用 getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID).我认为您的意思是调用 putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId).但是,如上所述,这也不起作用,因为您没有在 onEnabled() 中获得 id.
  1. 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 an appWidgetId - it's not a callback associated with a particular instance of an app widget on the home screen.
  2. You are calling getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID) on an Intent you've just created. I think you meant to call putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId). However, as mentioned above, this won't work either since you aren't given an id in onEnabled().

如果要在主屏幕上设置与每个 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.

  1. 完成配置应用小部件后,将其 appWidgetId 存储在 SharedPreferences 中(例如,您可以使用键appwidgetid_##"来存储布尔值).
  2. onUpdate() 被调用并且您正在迭代 appWidgetIds 数组时,首先检查 SharedPreferences 中的每个 appWidgetId.如果检查通过,您就知道用户已经配置了该 appWidget,您可以为其创建和设置闹钟;否则,continue 到下一个 appWidgetId.
  3. 设置闹钟的时候,注意Intent在创建PendingIntent的时候必须是唯一的,否则你会得到一个PendingIntent会复用或覆盖旧的(取决于您指定为 PendingIntent 调用的最后一个参数的标志).由于在检查唯一性时不考虑额外内容,请参阅底部的代码了解如何使其唯一性.
  4. onDelete() 中,取消该 appwidget 的警报.确保以完全相同的方式构造 PendingIntent.您还可以在此处从 SharedPreferences 中删除 appWidgetId.
  1. 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).
  2. When onUpdate() is called and you are iterating over the appWidgetIds array, check each appWidgetId 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 next appWidgetId.
  3. When setting the alarm, note that Intents must be unique when creating PendingIntents, otherwise you'll get a PendingIntent that reuses or overwrites the old one (depending on which flag you specify as the last argument to the PendingIntent call). Since extras are not considered when checking for uniqueness, see the code at the bottom for how to make it unique.
  4. In onDelete(), cancel the alarm for that appwidget. Make sure you construct the PendingIntent the exact same way. You can also remove the appWidgetId 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

基础教程推荐