问题检索额外的意图

时间:2011-09-11 17:30:49

标签: android android-intent

我有一个关闭警报的以下代码......

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

}

警报正确关闭,但由于某种原因没有获取“数据”额外信息,并且被设置为空。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

尝试:

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

答案 1 :(得分:0)

getExtra from Intent launched from a pendingIntent

可能重复

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

答案 2 :(得分:0)

引用来自putExtra函数的文档:

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

难道不是这个原因吗?