首页 > 编程知识 正文

android实现定时重试,android实现电脑开机

时间:2023-05-04 08:20:46 阅读:210609 作者:4295

最近项目要实现定时开机的功能,现在做个笔记记录一下,以防以后忘记,代码如下:
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);
}
}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。