set the time in Alarm manager Android - alarm fired instantly [duplicate]

别等时光非礼了梦想. 提交于 2019-12-10 10:06:41

问题


Possible Duplicate:
Why is my android alarm manager firing instantly?

I have this code which will call alarm notification

public static  Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.HOUR_OF_DAY,hour);
cal.add(Calendar.MINUTE, min);
Intent intent = new Intent(this,  OnetimeAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() , pendingIntent); 
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

but the alarm is fired instantly , it dosent wait after the given hour and minutes ? should I add anything to to the manifest file ?


回答1:


You use the current time to set the alarm. So it fires instantly.

Check the API. http://developer.android.com/reference/android/app/AlarmManager.html#set%28int,%20long,%20android.app.PendingIntent%29

There you pass the time when the alarm goes of as the second parameter. In your case this is the actual time. So you should add the amount of time you want to wait to the time your passing in the method right now.



来源:https://stackoverflow.com/questions/4003892/set-the-time-in-alarm-manager-android-alarm-fired-instantly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!