【问题标题】:getExtra from Intent launched from a pendingIntentgetExtra from Intent 从一个 pendingIntent 启动
【发布时间】:2011-02-22 08:55:49
【问题描述】:

在用户从列表中选择带有时间的内容并在给定时间为其创建通知后,我试图发出一些警报。我的问题是广播接收器无法接收到我的 Intent 上的 putExtra 的“showname”。它总是得到空值。 这是我为大多数意图执行此操作的方式,但我认为这一次可能是因为 pendingIntent 或 broadcastReceiver 需要以不同的方式完成某些事情。 谢谢你

通过待处理意图发送 Intent 的函数

public void setAlarm(String showname,String time) {

    String[] hourminute=time.split(":");
    String hour = hourminute[0];
    String minute = hourminute[1];
    Calendar rightNow = Calendar.getInstance();
    rightNow.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
    rightNow.set(Calendar.MINUTE, Integer.parseInt(minute));
    rightNow.set(Calendar.SECOND, 0);
    long t=rightNow.getTimeInMillis();
    long t1=System.currentTimeMillis();

    try {   

    Intent intent = new Intent(this, alarmreceiver.class);  
    Bundle c = new Bundle();            
    c.putString("showname", showname);//This is the value I want to pass
    intent.putExtras(c);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 12345, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(),pendingIntent);
    //Log.e("ALARM", "time of millis: "+System.currentTimeMillis());
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        Log.e("ALARM", "ERROR IN CODE:"+e.toString());
    }
}

这是接收端

public class alarmreceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();      
    Bundle b = intent.getExtras();
    String showname=b.getString("showname");//This is where I suppose to receive it but its null
    NotificationManager manger = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.icon,
            "TVGuide Υπενθύμιση", System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, tvguide.class), 0);

    notification.setLatestEventInfo(context, "Το Πρόγραμμα Ξεκίνησε",
            showname, contentIntent);

    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;

    notification.sound = Uri.parse("file:///sdcard/dominating.mp3");
    notification.vibrate = new long[]{100, 250, 100, 500};
    manger.notify(1, notification);
}           
}

【问题讨论】:

    标签: android android-intent bundle alarm


    【解决方案1】:

    如果您在 Intent 中更改 Extra 的值,那么在创建待处理 Intent 时,您应该使用标志 PendingIntent.FLAG_CANCEL_CURRENT。

    一个简单的例子是

    PendingIntent pi = PendingIntent.getBroadcast(context, 0,intentWithNewExtras,PendingIntent.FLAG_CANCEL_CURRENT);
    

    这是正确的方法,将确保您的新价值观得到传递。

    希望对你有帮助。

    【讨论】:

    • 这似乎不适用于带有鳍状肢的小部件,一旦启动意图,系统就会尝试重新利用它并且没有任何额外的更改。
    • 为我工作,我被困了好几个小时!
    • 像往常一样访问意图包?
    【解决方案2】:

    意图在系统中被重用,除非它们在我认为的上下文/动作上有所不同。 Documentation Link。也就是说,如果您已经构建了一个 Intent,那么以后也可能会使用该 Intent。

    作为调试测试,您可以尝试在intent.putExtras(c) 下方添加intent.setAction("" + Math.random()),看看另一端是否收到您的附加信息。

    相关文档

    由于这种行为,重要的是要知道两个 Intent 何时被认为是相同的,以便检索 PendingIntent。人们犯的一个常见错误是创建多个 PendingIntent 对象,其 Intent 仅在其“额外”内容上有所不同,期望每次都获得不同的 PendingIntent。这不会发生。 Intent 中用于匹配的部分与 Intent.filterEquals 定义的部分相同。如果您使用两个根据 Intent.filterEquals 等效的 Intent 对象,那么您将获得相同的 PendingIntent。

    【讨论】:

    • +1,这是正确的答案,应该被接受。真的很有用,谢谢。 (顺便说一句,您还可以在创建待处理意图时使用不同的请求代码,使用哈希,非常方便)。
    【解决方案3】:

    对不同的报警通知使用不同的请求代码,避免覆盖相同的报警时间。

    【讨论】:

    • 这是此类问题的最佳答案!一个问题的非常简单和漂亮的解决方法。
    【解决方案4】:

    您应该使用intent.putExtra() 方法。未将捆绑包设置为附加部分。如果你设置了字符串,你应该intent.putExtra(<key>, value);试试这个它会完成的。我用过。

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。我按照@aioobe 在这里的建议设置了一个动作来解决它,我的意图就像魔法一样工作。

      我做了什么

        ` intent.putExtra("Name", mName);
          intent.putExtra("dateTime", newdate);
          intent.setAction("" + Math.random());`
      

      希望它对某人有所帮助,编码愉快..! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-06
        • 1970-01-01
        • 1970-01-01
        • 2014-08-31
        • 1970-01-01
        • 2014-08-07
        • 2019-10-08
        • 2013-06-15
        相关资源
        最近更新 更多