android11代码关机
android10代码关机之前有些版本可以使用如下方式从代码里面调用关机:Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");i.putExtra("android.intent.extra.KEY_CONFIRM", false); //是否需要确认i.setFlags(Intent.FLAG_ACTIVI
·
android11代码关机
之前有些版本可以使用如下方式从代码里面调用关机:
Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", false); //是否需要确认
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
最近搞android11的项目此方法已经不能使用,
于是就使用如下反射的方式了:
//关机
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
Class clazz = pm.getClass();
try{
Method shutdown = clazz.getMethod("shutdown",boolean.class,String.class,boolean.class);
shutdown.invoke(pm,false,"shutdown",false);
}catch (Exception ex){
ex.printStackTrace();
}
另外还有一种方式是使用命令 reboot -p ,个人不建议大家使用。
不管上面哪一个都是需要在AndroidManifest.xml里面添加 :android:sharedUserId=“android.uid.system”
更多推荐
所有评论(0)