检索额外意图时出现问题

发布于 2024-12-04 04:34:35 字数 1237 浏览 1 评论 0原文

我有以下一次性警报代码...

        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
            Bundle bun = new Bundle();
            bun.putString("data", "hello this is my message...");
            intent.putExtras(bun);
            PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this,
                    0, intent, 0);

            // We want the alarm to go off 5 seconds from now. TODO
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 5);

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
            return false;
        }
    });
}



public class OneShotAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO
    Toast.makeText(context, "Alarm! " + intent.getExtras().getString("data"), Toast.LENGTH_SHORT).show();
}

}

警报正确响起,但由于某种原因未检索“数据”额外内容,并且被设置为空。

感谢您的帮助!

I have the following code for a one off alarm...

        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
            Bundle bun = new Bundle();
            bun.putString("data", "hello this is my message...");
            intent.putExtras(bun);
            PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this,
                    0, intent, 0);

            // We want the alarm to go off 5 seconds from now. TODO
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 5);

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
            return false;
        }
    });
}



public class OneShotAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO
    Toast.makeText(context, "Alarm! " + intent.getExtras().getString("data"), Toast.LENGTH_SHORT).show();
}

}

The alarm correctly goes off, but the "data" extra isn't getting retrieved for some reason, and is being set to null.

Thanks for the help!

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

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

发布评论

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

评论(3

红玫瑰 2024-12-11 04:34:36

尝试:

Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
intent.putExtra("data", "hello this is my message...");
PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Try:

Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
intent.putExtra("data", "hello this is my message...");
PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
半城柳色半声笛 2024-12-11 04:34:36

可能重复 getExtra from Intent launch from aendingIntent

如果没有的话如果没有帮助,请告诉我们更多有关您的文件的信息(您需要有一个设置闹钟的活动、启动服务的广播接收器以及告诉应用程序做什么的服务当闹钟响起时)

Possible duplicate of getExtra from Intent launched from a pendingIntent

If that doesn't help, please tell us a bit more about your files (you need to have an activity where you set up the alarm, a broadcastreceiver that starts a service, and the service where you tell the app what to do when the alarm goes off)

_失温 2024-12-11 04:34:36

putExtra 函数的文档引用:

name 额外数据的名称,带有包前缀

不会是这个原因吧?

Citation from the doc for putExtra function:

name The name of the extra data, with package prefix.

Couldn't it be the reason?

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