在特定时间设置 Android 闹钟
我在特定时间设置了闹钟,但每次我打开应用程序时它都会打开 这是我使用的代码:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0010000,intent,0);
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, 5);
time.set(Calendar.MINUTE, 59);
time.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC,time.getTimeInMillis(),pendingIntent);
i set the alarm at specific time but every time i open the application it will turn on
this is the code i used :
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0010000,intent,0);
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, 5);
time.set(Calendar.MINUTE, 59);
time.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC,time.getTimeInMillis(),pendingIntent);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,您需要将闹钟设置为下次响铃时间为 5:59:00。
您可以通过获取当前时间来完成此操作,如果在 5:59:00 之前,则设置闹钟,如果在 5:59:00 之后,则添加一天并设置闹钟。这样做:
我想给你一个可构建的例子,但我还没有完全理解alarmmanager,所以这就是你所拥有的。基于 eclipse 3.5.2 和 ADK 15 构建
Alright, you need to set the alarm to ring the next time it is 5:59:00.
You do this by getting the current time, if its before 5:59:00, set the alarm, if its after 5:59:00 then add a day and set the alarm. Do it like so:
I wanted to give you a buildable example, but i don't fully understand alarmmanager yet, so this is what you have. Built on eclipse 3.5.2 with ADK 15
不要将此代码放置在 onCreate 函数中,而是将其放置在任何按钮事件或仅在用户交互时调用的任何其他事件中。
我想,这样就可以了。
将此“取消”方法添加到您的代码中。这将在设置新警报之前取消上一个警报。
Instead of placing this code in onCreate function, place it inside any button event or any other event which will be called only on user interaction.
I think, that'll do.
add this 'cancel' method to your code. this will cancel the previous alarm before setting the new one.