1. 程式人生 > >Android開發實現關機或重啟

Android開發實現關機或重啟

//關機
    private void shutdown(){
        try{
            Class ServiceManager = Class.forName("android.os.ServiceManager");
            Method getService =  ServiceManager.getMethod("getService",java.lang.String.class);
            Object object = getService.invoke(null,Context.POWER_SERVICE);
Class cStub = Class.forName("android.os.IPowerManager$Stub"); Method asInterface = cStub.getMethod("asInterface",android.os.IBinder.class); Object oIPower = asInterface.invoke(null,object); Method shutdown = oIPower.getClass().getMethod("shutdown",boolean
.class,boolean.class); shutdown.invoke(oIPower,false,true); }catch (Exception e){ Log.e(TAG, Log.getStackTraceString(e)); } }
//重啟
Intent iReboot = new Intent(Intent.ACTION_REBOOT);
iReboot.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
__mainActivity.startActivity
(iReboot);