android8.0 定时关机休眼的功能实现
一、首先创建一个定时任务/*** 开启定时关机广播* @param context* @param time*/public void setShutdown(Context context,long time){Log.e("SPGJSJActivity","SetShutdown ="+time);AlarmManager am = (AlarmManager) context.getSyst
·
一、首先创建一个定时任务
/**
* 开启定时关机广播
* @param context
* @param time
*/
public void setShutdown(Context context,long time){
Log.e("SPGJSJActivity","SetShutdown ="+time);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent();
intent.setClass(context, ShutdownBroadcastReceiver.class);
intent.putExtra("flag", "shutdown");
intent.setAction("101");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 101, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if(Build.VERSION.SDK_INT < 19){
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +time, pendingIntent);
}else{
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +time, pendingIntent);
}
}
注:Alarm 的方法 set() 在SDK_INT 19以前是精确的闹钟
19以后为了节能省电(减少系统唤醒和电池使用)。
使用Alarm.set()和Alarm.setRepeating()已经不能保证精确性。
19以后需要精确的闹钟就需要用到Alarm方法setWindow()和setExact()这两个方法了
二、创建广播,定时任务指定时间开启广播执行关机动作
public class ShutdownBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
String flag = intent.getExtras().getString("flag");
if(flag.equals("shutdown")){
Log.e("shutdownBroadcast","时间到-->执行关机动作");
shutdownSys(context);
}
} catch (Exception e){
e.printStackTrace();
}
}
/**
* 关机(需要系统权限)
* @param context
*/
public static void shutdownSys(Context context) {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.setAction("com.android.internal.intent.action.REQUEST_SHUTDOWN");
} else {
intent.setAction("android.intent.action.ACTION_REQUEST_SHUTDOWN");
}
// 其中false换成true,会弹出是否关机的确认窗口
intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
注:8.0之前的版本,系统的manifest中的注册信息
action为:android.intent.action.ACTION_REQUEST_SHUTDOWN
8.0版本,系统的manifest中的注册信息
action为:com.android.internal.intent.action.REQUEST_SHUTDOWN
添加关机权限
<uses-permission android:name="android.permission.SHUTDOWN" />
将APP提升至系统权限
android:sharedUserId="android.uid.system"
三、休眼功能
首先申请权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
android 版本 9.0 需要动态在申请一次权限
/**
* 检测是否拥有写入系统权限的权限
*/
private void showDialog(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //版本在23及以上
if (!Settings.System.canWrite(this)) {
final CommonDialog deleteDialog = new CommonDialog(mContext);
deleteDialog.setTitle("应用需要您授权\n\"修改系统设置\"的权限");
deleteDialog.setPositiveButton(R.string.confirm,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
deleteDialog.cancel();
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
});
deleteDialog.setNegativeButton(R.string.cancel,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
deleteDialog.cancel();
finish();
}
});
deleteDialog.setCancelable(false); //返回键还是会关闭Dialog
deleteDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0) {
return true; //返回true会截断返回键的点击事件,不做其他处理
} else {
return false;
}
}
});
deleteDialog.show();
}
}
}
调用api执行休眠的功能
if(selectedIndex == 0){
//永不休眠
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,Integer.MAX_VALUE);
}else if(selectedIndex == 1){
Settings.System.putLong(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1*60*1000);
}else if(selectedIndex == 2){
Settings.System.putLong(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 5*60*1000);
}else if(selectedIndex == 3){
Settings.System.putLong(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 10*60*1000);
}
注:时长是单位是毫秒
更多推荐
已为社区贡献3条内容
所有评论(0)