Android 设置定时闹铃的完整解决方案,适配到Android13

之前在做定位的时候,遇到一个问题,在华为的pad上黑屏超过一小时就获取不到定位权限的问题,试了很多种方法,即使是使用长连接保持活跃也不行,最后没办法了,做了定时闹铃点亮屏幕。

我想要的效果是在一天的某个时间段,一小时定时点亮一次。像这种长期定时的需求,还是用系统闹铃来做比较合适。

权限

java 复制代码
<uses-permission android:name="android.permission.WRITE_SETTINGS"

加这个权限是为了适配Android12以上机器

1.创建闹铃

java 复制代码
Intent intent = new Intent(this, WakeupReceiver.class);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        PendingIntent pendingIntent;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
            pendingIntent =
                    PendingIntent.getBroadcast(this, 0, intent,
                            PendingIntent.FLAG_IMMUTABLE);
        } else {
            pendingIntent =
                    PendingIntent.getBroadcast(this, 0, intent,
                            0);
        }
        if (alarmManager!=null&&pendingIntent!=null){
            alarmManager.cancel(pendingIntent);
        }
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+interval, pendingIntent);
        

setexact 创建一次闹铃,并且在在创建之前取消已经设置过的,在接收到闹铃回调的地方再调下次闹铃。这是目前系统给出的长时间重复闹铃的最好解决方法,Android19以后setrepeat不稳定,这个我也尝试过了,定了个一小时的闹铃,结果十分钟就响应了,很坑。

2.接收闹铃并回调

java 复制代码
public class WakeupReceiver extends BroadcastReceiver {
    @SuppressLint("ScheduleExactAlarm")
    @Override
    public void onReceive(Context context, Intent intent) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.HOUR_OF_DAY, 20); // 设置开始时间为早上8点
        calendar2.set(Calendar.MINUTE, 30);
        calendar2.set(Calendar.SECOND, 0);
        long endTime = calendar2.getTimeInMillis(); // 获取结束时间的毫秒数
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 7); // 设置开始时间为早上8点
        calendar.set(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND, 0);


        long startTime = calendar.getTimeInMillis(); // 获取开始时间的毫秒数
        if (System.currentTimeMillis() <= endTime && startTime < System.currentTimeMillis()) {
            // 在这里编写您要执行的动作代码
            // 这里会在每个小时触发
            AliveService.wakeUpAndUnlock();
            LocationUtils.updateLocationnodata("唤醒");
        }
        Intent intent2 = new Intent(context, WakeupReceiver.class);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        PendingIntent pendingIntent;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
            pendingIntent =
                    PendingIntent.getBroadcast(context, 0, intent2,
                            PendingIntent.FLAG_IMMUTABLE);
        } else {
            pendingIntent =
                    PendingIntent.getBroadcast(context, 0, intent2,
                            0);
        }
        if (alarmManager!=null&&pendingIntent!=null){
            alarmManager.cancel(pendingIntent);
        }
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+interval, pendingIntent);
       

做了时间约束,早上七点半到晚上8点半才会点亮屏幕,其他时间不处理,然后重新再发一次闹铃。闹铃时间间隔我用了常量配置

复制代码
public static final long interval = 1 * 60 * 1000 * 60;

3.在清单文件种配置

java 复制代码
<receiver
            android:name=".keeepalive.WakeupReceiver"
            android:enabled="true"
            android:exported="false" />

至此,闹铃配置完成,测试通过。

相关推荐
nono牛3 分钟前
安卓/MTK平台日志关键词详解
android
TimeFine1 小时前
Android AI解放生产力(四)实战:解放绘制UI的繁琐工作
android
sheji34162 小时前
【开题答辩全过程】以 基于Android的社区车位共享管理系统的设计与实现为例,包含答辩的问题和答案
android
TimeFine2 小时前
Android AI解放生产力(三):认识custom_prompts和skills
android
summerkissyou19872 小时前
Android-Audio-为啥不移到packages/module
android·音视频
catchadmin2 小时前
PHP 值对象实战指南:避免原始类型偏执
android·开发语言·php
BoomHe2 小时前
Android 键盘事件导致页面产生「 半透明蒙层」
android
用户69371750013842 小时前
29.Kotlin 类型系统:智能转换:类型检查 (is) 与类型转换 (as)
android·后端·kotlin
用户69371750013842 小时前
30. Kotlin 扩展:为“老类”添“新衣”:扩展函数与扩展属性
android·后端·kotlin
TimeFine2 小时前
Android AI解放生产力(二):认识MCP以及配置config.toml
android