android 9.0定时开机的实现

本文介绍了如何在Android 9.0系统上实现定时开机功能。通过设置AlarmManager和发送特定的Intent,可以达到定时开机的效果。在SDK版本大于N_MR1时,使用特定的Intent包名;否则,利用ACTION_BOOT_COMPLETED和AlarmManager进行设置。
摘要由CSDN通过智能技术生成

最近项目要实现定时开机的功能,现在做个笔记记录一下,以防以后忘记,代码如下:
private void setPowerOnAlarm(Context mContext) {
//定时开机时间,时间设置最好不要小于一分钟,我此处设置为90 * 1000毫秒
long time = System.currentTimeMillis() + 90 * 1000;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
Intent intent = new Intent(“org.codeaurora.poweroffalarm.action.SET_ALARM”);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.setPackage(“com.qualcomm.qti.poweroffalarm”);
intent.putExtra(“time”, time);
sendBroadcast(intent);
} else {
Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED);
intent.putExtra(“seq”, 1);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(
Context.ALARM_SERVICE);
int type = 8;
try {
Field field = AlarmManager.class.getField(“RTC_POWEROFF_WAKEUP”);
type = field.getInt(null);
} catch (Exception e) {
//nothing to do
}
alarmManager.setExact(type, time, pi);
}
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓兼职framework应用工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值