从挂起的 Intent 启动的 Intent getExtra

发布于 2024-09-02 14:08:26 字数 2490 浏览 13 评论 0原文

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

通过pending Intent发送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);
}           
}

I am trying to make some alarms after the user selects something with a time from a list and create a notification for it at the given time. My problem is that the "showname" that a putExtra on my Intent cant be received at the broadcast receiver. It always get null value.
This is the way I do it for most of my intents but I think this time maybe because of the pendingIntent or the broadcastReceiver something need to be done differentelly.
Thank you

The function that sends the Intent through the pending 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());
    }
}

And this is the receiving end

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);
}           
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

舂唻埖巳落 2024-09-09 14:08:26

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

一个简单的例子是,

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

这是正确的方式,将确保交付您的新价值观。

希望有帮助。

If you change the Extra's value in the intent, then while creating the pending intent you should use the flag PendingIntent.FLAG_CANCEL_CURRENT.

A simple example would be

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

This is the right way and will ensure that your new values are delivered.

Hope it helps.

寂寞美少年 2024-09-09 14:08:26

意图在系统中被重用,除非它们在上下文/操作上有所不同,我相信。 文档链接。也就是说,如果您已经构建了一个 Intent,则该 Intent 也可能在以后使用。

作为调试测试,您可以尝试在 intent.putExtras(c) 下面添加 intent.setAction("" + Math.random()) 并查看您的额外功能被另一端收到。

相关文档

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

Intents are reused in the system, unless they differ on context/action I believe. Documentation Link. That is, if you have already constructed an Intent, that intent might be used later as well.

As a debug-test, you could try to add intent.setAction("" + Math.random()) below intent.putExtras(c) and see if your extras are received in the other end.

Relevant Documentation:

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

夜唯美灬不弃 2024-09-09 14:08:26

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

use different request code for different alarm notifications to avoid overwriting of same alarm time.

剩余の解释 2024-09-09 14:08:26

您应该使用 intent.putExtra() 方法。未将捆绑包设置为 extras 段。如果你设置了字符串,你应该 intent.putExtra(, value); 尝试一下它就会完成。我用过。

You should use intent.putExtra() method. Not set bundle to extras segment. IF you set string, you whould intent.putExtra(<key>, value); Try this it will be done. I used it.

雪若未夕 2024-09-09 14:08:26

我也遇到了同样的问题。我按照 @aioobe 的建议,通过设置意图动作来解决这个问题,我的意图就像魔术一样。

这是我所做的

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

希望它能帮助别人,快乐编码......! :)

I was had same problem. I solved it with setting an action to intent as suggested by @aioobe here, and my intent works like a magic.

Here what i did

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

Hope it will help someone, happy coding..! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文