从获得的BroadcastReceiver部件编号 [英] Get widget id from BroadcastReceiver

查看:169
本文介绍了从获得的BroadcastReceiver部件编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道的小部件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

推荐答案

有几件事情...

  1. onEnabled 在AppWidgetProvider只有当第一appwidget加入调用一次(在此AppWidgetProvider变为已启用这是)。请注意,onEnabled不给你的 appWidgetId - 这是不是与主屏幕上的应用程序窗口小部件的特定实例相关联的回调
  2. 您正在呼叫 getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)在意向刚刚创建。我想你的意思是叫 putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId)。然而,正如上面提到的,这也不行,因为你没有给出一个ID在 onEnabled()
  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 共享preferences(你可以使用键appwidgetid _ ##来存储一个布尔值,例如)。
  2. 的OnUpdate()被调用,你遍历 appWidgetIds 数组,检查每个 appWidgetId 共享preferences第一。如果检查通过,你知道用户已经配置了appWidget,你可以创建和设置你的闹钟吧;否则,继续 appWidgetId
  3. 当设置闹钟,请注意,意图创建 PendingIntent S当s必须是唯一的,否则你会获得 PendingIntent 一个可重用或覆盖旧的(根据您指定作为最后一个参数到标志上的 PendingIntent 呼叫)。由于额外检查的独特性时,不考虑,看到code底部的如何使它独一无二的。
  4. onDelete(),取消报警的appwidget。请确保您构建PendingIntent完全相同的方式。您还可以在这里删除 appWidgetId 从共享preferences。
  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部件编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆