From 18f2f384da361c0d6199225ce32eaf41572b1813 Mon Sep 17 00:00:00 2001 From: snickersun Date: Fri, 22 Nov 2019 20:05:55 +0800 Subject: [PATCH] =?utf8?q?=E6=95=B4=E7=90=86=E6=B3=A8=E9=87=8A=E5=85=B3?= =?utf8?q?=E6=9C=BA=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/mobile/miki/shutdown_activity.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/mobile/miki/shutdown_activity.java b/app/src/main/java/mobile/miki/shutdown_activity.java index 1b9bf78..3f07f12 100644 --- a/app/src/main/java/mobile/miki/shutdown_activity.java +++ b/app/src/main/java/mobile/miki/shutdown_activity.java @@ -1,14 +1,17 @@ package mobile.miki; +import android.content.Context; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; +import java.lang.reflect.Method; + public class shutdown_activity extends BaseActivity { - String a=""; + String TAG="shutdown_activity"; Button btn_yes; Button btn_no; @Override @@ -28,17 +31,31 @@ public class shutdown_activity extends BaseActivity { //log("shutdown"); try { - Object object = Class.forName("android.os.ServiceManager").getMethod("getService", new Class[] { String.class }).invoke(null, new Object[] { "power" }); - object = Class.forName("android.os.IPowerManager$Stub").getMethod("asInterface", new Class[] { android.os.IBinder.class }).invoke(null, new Object[] { object }); - if (Build.VERSION.RELEASE.contains("7.0")) { - object.getClass().getMethod("shutdown", new Class[] { boolean.class, String.class, boolean.class }).invoke(object, new Object[] { Boolean.valueOf(false), null, Boolean.valueOf(true) }); - return; - } - object.getClass().getMethod("shutdown", new Class[] { boolean.class, boolean.class }).invoke(object, new Object[] { Boolean.valueOf(false), Boolean.valueOf(true) }); - return; - } catch (Exception exception) { - Log.e(a, exception.toString(), exception); - return; + + //获得ServiceManager类 + Class ServiceManager = Class + .forName("android.os.ServiceManager"); + + //获得ServiceManager的getService方法 + Method getService = ServiceManager.getMethod("getService", java.lang.String.class); + + //调用getService获取RemoteService + Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE); + + //获得IPowerManager.Stub类 + Class cStub = Class + .forName("android.os.IPowerManager$Stub"); + //获得asInterface方法 + Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class); + //调用asInterface方法获取IPowerManager对象 + Object oIPowerManager = asInterface.invoke(null, oRemoteService); + //获得shutdown()方法 + Method shutdown = oIPowerManager.getClass().getMethod("shutdown",boolean.class,boolean.class); + //调用shutdown()方法 + shutdown.invoke(oIPowerManager,false,true); + + } catch (Exception e) { + Log.e(TAG, e.toString(), e); } } }); -- 2.17.1