gpt4 book ai didi

Android 设备重启检查指定的报警时间是否过去

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:36 26 4
gpt4 key购买 nike

我需要你的帮助。我被这个问题困住了。警报正常工作。但是,当设备重启时,db中存储的指定时间是过去(下午3点),当前时间是下午4点,如何检查以防止立即触发警报?

Docs说:

If the stated trigger time is in the past, the alarm will be triggered immediately. Here's what I've tried so far:

class DeviceBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

// Check if successful reboot
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

SharedPreferences shared = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
// Check if daily notification is enabled
// If yes, call NotificationPublisher to communicate with NotificationAlarmService
if(shared.getBoolean(Constants.KEY_IS_DAILY_NOTIFIED, false)) {
DBHelper dbHelper = new DBHelper(context);
DBConnector.dbConnect(dbHelper);

int DAILY_NOTIFICATION_ID = Constants.DAILY_VERSE_NOTIFICATION_1_ID;
ArrayList<Notification> notificationList = dbHelper.getNotifications();
Log.e("", "notificationList: " + notificationList.size());

for(Notification obj : notificationList) {
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, obj.getHourOfDay());
datetime.set(Calendar.MINUTE, obj.getMinute());
datetime.set(Calendar.SECOND, 0);

Calendar now = Calendar.getInstance();
if (now.after(datetime)) {
datetime.add(Calendar.DATE, now.get(Calendar.DATE) + 1);
}

Log.e("BOOT RECEIVER", "" + obj.getHourOfDay() + ":" + obj.getMinute());

Intent myIntent = new Intent(context, NotificationPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
DAILY_NOTIFICATION_ID++,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
datetime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}

}

}
}

安卓 list :

<receiver
android:name=".utils.DeviceBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>

我的代码似乎有什么问题?我将不胜感激任何帮助。谢谢!

最佳答案

我像这样设置和检查闹钟时间,它现在对我有用:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Calendar now = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
if (calendar.before(now))
calendar.add(Calendar.DAY_OF_MONTH, 1);

关于Android 设备重启检查指定的报警时间是否过去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34651995/

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