我正在尝试创建一个任务调度程序。现在我只想在收到预定的广播后弹出Toast消息。所以我有这个BroadcastReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int group_id = intent.getIntExtra("SELECTED_GROUP", 0);
Toast.makeText(context, "Broadcast received. Selected group ID: "+group_id,
Toast.LENGTH_LONG).show();
}
}
这就是我在AndroidManifest文件中的内容:
<receiver android:name="com.ikmedia.smscheduler.receivers.AlarmReceiver">
<intent-filter >
<action android:name="com.mysite.myapp.sendSchedule"/>
</intent-filter>
</receiver>
在我的活动中,当用户点击“保存”按钮时,这就是我所拥有的:
Calendar calendar_schedule_time = Calendar.getInstance();
calendar_schedule_time.set(selected_year, selected_month, selected_day, selected_hour, selected_minute);
Intent intent_scheduler = new Intent();
intent_scheduler.setAction("com.mysite.myapp.sendSchedule");
intent_scheduler.putExtra("SELECTED_GROUP", selected_group_id);
PendingIntent pi_schedule_intent = PendingIntent.getBroadcast(this, Constants.Request_Codes.REQUEST_CODE_SEND_SCHEDULE, intent_scheduler, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm_manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar_schedule_time.getTimeInMillis(), pi_schedule_intent);
然而,当我点击“保存”按钮时 - 我没有收到预定的警报,而是来自BroadcastReceiver的即时响应。我弹出Toast消息,带有Extra文本,但它立即发生,我没有时间表。
为什么?我该如何解决?
答案 0 :(得分:0)
alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar_schedule_time.getTimeInMillis(), pi_schedule_intent);
将上述声明替换为: -
alarm_manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + calendar_schedule_time.getTimeInMillis(), pi_schedule_intent);