Android 11.0 接口方式实现 关机和重启

frameworks/base/…/MdmPolicy.java

写好接口供上层调用

    public void shutingdown(){
    
    
        RebootShutdownControl.doshutdown(mContext);
    }

    public void reboot(){
    
    
        android.util.Log.d(TAG,"reboot()",new Exception());
        RebootShutdownControl.doreboot(mContext);
    }

frameworks/base/…/RebootShutdownControl.java

具体实现

class RebootShutdownControl {
    
    
    public static void doreboot(Context context) {
    
    
        IPowerManager mPowerManager = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
        try{
    
    
            mPowerManager.reboot(false, null, true);
        }catch (Exception e){
    
    
            Log.d("RebootShutdownControl","重启失败");
        }
    }

    public static void doshutdown(Context context) {
    
    
        IPowerManager mPowerManager = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
        try{
    
    
            mPowerManager.shutdown(false, null, false);
        }catch (Exception e){
    
    
            Log.d("RebootShutdownControl","关机失败");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_27494201/article/details/125196759