gpt4 book ai didi

android - 在特定时间设置安卓闹钟

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:05 25 4
gpt4 key购买 nike

我在特定时间设置了闹钟,但每次我打开应用程序时它都会打开这是我使用的代码:

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);

最佳答案

好的,您需要将闹钟设置为下次 5:59:00 响铃。您可以通过获取当前时间来执行此操作,如果它在 5:59:00 之前,则设置闹钟,如果它在 5:59:00 之后,则添加一天并设置闹钟。这样做:

import java.util.Calendar;
import java.util.Date;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));

Date dat = new Date();//initializes to now
Calendar cal_alarm = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal_now.setTime(dat);
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY,5);//set the alarm time
cal_alarm.set(Calendar.MINUTE, 59);
cal_alarm.set(Calendar.SECOND,0);
if(cal_alarm.before(cal_now)){//if its in the past increment
cal_alarm.add(Calendar.DATE,1);
}
//SET YOUR AlarmManager here

}

我想给你一个可构建的例子,但我还没有完全理解 alarmmanager,所以这就是你所拥有的。构建于带有 ADK 15 的 eclipse 3.5.2

关于android - 在特定时间设置安卓闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8139936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com