以下是我在代码中尝试的内容。多个警报无法正常运行Android
AlarmManager alarmManager = ((AlarmManager) getSystemService(Context.ALARM_SERVICE));
int time1 = Integer.parseInt(e.getText().toString());
time1 = time1 * 1000;
final int id = (int) System.currentTimeMillis();
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
long r4 = time1 + System.currentTimeMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, r4, pendingIntent);
任何人都知道出了什么问题, 一些帮助将不胜感激,谢谢:)
答案 0 :(得分:0)
如何分配触发警报alarmManager.set(AlarmManager.RTC_WAKEUP,r4, pendingIntent);
的时间是错误的。
需要在Calendar
中设置您从用户获得的时间(秒),然后在alarmManager.set();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, time1);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
您的代码就像,
AlarmManager alarmManager;
alarmManager=( (AlarmManager)getSystemService(Context.ALARM_SERVICE));
int time1 = Integer.parseInt(e.getText().toString());
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, time1);
final int _id = (int) System.currentTimeMillis();
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,_id , intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);