Android | 使用 dumpsys alarm 验证自己应用使用的 Alarm 是否正确

我们可以使用 android.app.AlarmManagerAndroid 中执行定时任务,并可以重复执行。例如:
我们首先获取到 AlarmManager

val alarmManager = context.getSystemService(Context.ALARM_SERVICE)) as AlarmManager

随后,我们可以使用 AlarmManager.setInexactRepeating() 方法执行定时任务

alarmManager.setInexactRepeating(AlarmManager.RTC, firstStartTime, intervalMillis, pendingIntent)

那么我们如何验证我们的定时任务是否设置成功呢?我们可以使用 dumpsys alarm 方法过滤自己的应用的包名,可以得到我们所有应用的 Alarm 信息:例如:

dumpsys alarm | grep teleostnacl
    RTC #75: Alarm{dcf3d9c type 1 origWhen 1758620272086 whenElapsed 852372564 com.teleostnacl.phonetoolbox}
      tag=*alarm*:com.teleostnacl.phonetoolbox.NotificationService.ATTACHMENT_SERVICE
      operation=PendingIntent{8aaf3a5: PendingIntentRecord{da4337a com.teleostnacl.phonetoolbox startService}}
    RTC #121: Alarm{58192c6 type 1 origWhen 1758643200000 whenElapsed 875300478 com.teleostnacl.phonetoolbox}
      tag=*alarm*:com.teleostnacl.phonetoolbox.NotificationService.UPDATE_NOTIFICATION
      operation=PendingIntent{e080287: PendingIntentRecord{59865b4 com.teleostnacl.phonetoolbox startService}}
    RTC #126: Alarm{4909b77 type 1 origWhen 1758646800000 whenElapsed 879175000 com.teleostnacl.phonetoolbox}
      tag=*alarm*:com.teleostnacl.phonetoolbox.NotificationService.ATTACHMENT_SERVICE
      operation=PendingIntent{5faa3e4: PendingIntentRecord{fff704d com.teleostnacl.phonetoolbox startService}}
    RTC #227: Alarm{58af202 type 1 origWhen 1759093200000 whenElapsed 1325425000 com.teleostnacl.phonetoolbox}
      tag=*alarm*:com.teleostnacl.phonetoolbox.NotificationService.ATTACHMENT_SERVICE
      operation=PendingIntent{171d713: PendingIntentRecord{8b28d50 com.teleostnacl.phonetoolbox startService}}

此时可以看到有类似 RTC #*: Alarm 开头的 Alarm 信息,而 其中的 origWhen 即是下一次执行的时间,这是一个 ms 级的时间戳,可以通过转换得到具体的时间,以便用来调试

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值