使用警报管理器将int传递给BroadcastReceiver类

时间:2015-11-19 16:36:09

标签: android android-intent alarmmanager

我试图通过闹钟手传递两个整数,但每当我传递它们时,吐司回复null。我无法理解问题所在。

Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);

        intent.putExtra("passedHour", cal.get(Calendar.HOUR_OF_DAY));
        intent.putExtra("passedMin", cal.get(Calendar.MINUTE));

        PendingIntent alarmIntent = PendingIntent.getBroadcast(
                MainActivity.this, 0, intent, 0);

        AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                alarmIntent);

这是alaramreceiver类中的接受者

Toast.makeText(context, "Alarm!! " + intent.getStringExtra("passedHour") + ":" + intent.getStringExtra("passedMin"), Toast.LENGTH_LONG).show();

1 个答案:

答案 0 :(得分:0)

cal.get()方法返回int,因此intent将值作为int发送 您需要使用intent.getIntExtra方法才能获得int 所以在onReceive中使用

Toast.makeText(context, "Alarm!! " + intent.getIntExtra("passedHour") + ":" + intent.getIntExtra("passedMin"), Toast.LENGTH_LONG).show();