feat: 增加整点报时,增加闹钟
This commit is contained in:
@@ -7,19 +7,57 @@ import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
|
||||
import cn.jpush.android.service.AlarmReceiver;
|
||||
import com.ttstd.dialer.db.alarm.AlarmInfo;
|
||||
import com.ttstd.dialer.db.alarm.AlarmRepository;
|
||||
import com.ttstd.dialer.db.alarm.IntegerListConverter;
|
||||
import com.ttstd.dialer.receiver.AlarmReceiver;
|
||||
import com.ttstd.dialer.utils.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.rxjava3.core.Completable;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public class AlarmManagerHelper {
|
||||
private static final String TAG = "AlarmManagerHelper";
|
||||
|
||||
public static final int ALARM_REQUEST_CODE = 1001;
|
||||
/**
|
||||
* 重新调度所有已启用的闹钟
|
||||
*
|
||||
* @param context 上下文
|
||||
*/
|
||||
public static void rescheduleAllAlarms(Context context) {
|
||||
AlarmRepository repository = new AlarmRepository(context);
|
||||
Completable.fromAction(() -> {
|
||||
List<AlarmInfo> enabledAlarms = repository.getEnabledAlarms();
|
||||
for (AlarmInfo alarm : enabledAlarms) {
|
||||
long nextTime = calculateNextTime(alarm);
|
||||
alarm.setNextTriggerTime(nextTime);
|
||||
repository.updateNextTriggerTime(alarm.getId(), nextTime);
|
||||
setExactAlarm(context, alarm.getId(), nextTime);
|
||||
Logger.d(TAG, "已重新调度闹钟 ID: " + alarm.getId() + ",下次触发时间: " + nextTime);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io()).subscribe();
|
||||
}
|
||||
|
||||
public static long calculateNextTime(AlarmInfo info) {
|
||||
AlarmRepeatConfig config = new AlarmRepeatConfig(info.getRepeatType());
|
||||
if (info.getRepeatType() == AlarmRepeatConfig.REPEAT_CUSTOM) {
|
||||
List<Integer> days = IntegerListConverter.toIntegerList(info.getCustomDays());
|
||||
config.setCustomDays(new ArrayList<>(days));
|
||||
}
|
||||
return AlarmTimeCalculator.calculateNextAlarmTime(info.getHour(), info.getMinute(), config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置一个精准闹钟(兼容 Android 5.0 到 Android 16)
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param alarmId 闹钟ID,用于区分不同的 PendingIntent
|
||||
* @param triggerAtMillis 触发的时间戳(毫秒)
|
||||
*/
|
||||
public static void setExactAlarm(Context context, long triggerAtMillis) {
|
||||
public static void setExactAlarm(Context context, int alarmId, long triggerAtMillis) {
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
if (alarmManager == null) return;
|
||||
|
||||
@@ -35,12 +73,14 @@ public class AlarmManagerHelper {
|
||||
}
|
||||
|
||||
Intent intent = new Intent(context, AlarmReceiver.class);
|
||||
intent.putExtra("ALARM_ID", alarmId);
|
||||
|
||||
// 使用 FLAG_IMMUTABLE 或 FLAG_MUTABLE 增强 Android 12+ 安全性
|
||||
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ?
|
||||
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE :
|
||||
PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ALARM_REQUEST_CODE, intent, flags);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, flags);
|
||||
|
||||
// 核心兼容性定时逻辑
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
@@ -57,15 +97,18 @@ public class AlarmManagerHelper {
|
||||
|
||||
/**
|
||||
* 取消闹钟
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param alarmId 闹钟ID
|
||||
*/
|
||||
public static void cancelAlarm(Context context) {
|
||||
public static void cancelAlarm(Context context, int alarmId) {
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
if (alarmManager != null) {
|
||||
Intent intent = new Intent(context, AlarmReceiver.class);
|
||||
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ?
|
||||
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE :
|
||||
PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ALARM_REQUEST_CODE, intent, flags);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, flags);
|
||||
alarmManager.cancel(pendingIntent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,10 @@ public class AlarmRepeatConfig {
|
||||
}
|
||||
}
|
||||
|
||||
public void setCustomDays(ArrayList<Integer> customDays) {
|
||||
this.customDays = customDays;
|
||||
}
|
||||
|
||||
public int getRepeatType() {
|
||||
return repeatType;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user