整理注释关机代码
authorsnickersun <scy@incartech.cn>
Fri, 22 Nov 2019 12:05:55 +0000 (20:05 +0800)
committersnickersun <scy@incartech.cn>
Fri, 22 Nov 2019 12:05:55 +0000 (20:05 +0800)
app/src/main/java/mobile/miki/shutdown_activity.java

index 1b9bf78..3f07f12 100644 (file)
@@ -1,14 +1,17 @@
 package mobile.miki;\r
 \r
+import android.content.Context;\r
 import android.os.Build;\r
 import android.os.Bundle;\r
 import android.util.Log;\r
 import android.view.View;\r
 import android.widget.Button;\r
 \r
+import java.lang.reflect.Method;\r
+\r
 public class shutdown_activity extends BaseActivity {\r
 \r
-    String a="";\r
+    String TAG="shutdown_activity";\r
     Button btn_yes;\r
     Button btn_no;\r
     @Override\r
@@ -28,17 +31,31 @@ public class shutdown_activity extends BaseActivity {
                 //log("shutdown");\r
 \r
                 try {\r
-                    Object object = Class.forName("android.os.ServiceManager").getMethod("getService", new Class[] { String.class }).invoke(null, new Object[] { "power" });\r
-                    object = Class.forName("android.os.IPowerManager$Stub").getMethod("asInterface", new Class[] { android.os.IBinder.class }).invoke(null, new Object[] { object });\r
-                    if (Build.VERSION.RELEASE.contains("7.0")) {\r
-                        object.getClass().getMethod("shutdown", new Class[] { boolean.class, String.class, boolean.class }).invoke(object, new Object[] { Boolean.valueOf(false), null, Boolean.valueOf(true) });\r
-                        return;\r
-                    }\r
-                    object.getClass().getMethod("shutdown", new Class[] { boolean.class, boolean.class }).invoke(object, new Object[] { Boolean.valueOf(false), Boolean.valueOf(true) });\r
-                    return;\r
-                } catch (Exception exception) {\r
-                    Log.e(a, exception.toString(), exception);\r
-                    return;\r
+\r
+                    //获得ServiceManager类\r
+                    Class  ServiceManager = Class\r
+                            .forName("android.os.ServiceManager");\r
+\r
+                    //获得ServiceManager的getService方法\r
+                    Method getService = ServiceManager.getMethod("getService", java.lang.String.class);\r
+\r
+                    //调用getService获取RemoteService\r
+                    Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE);\r
+\r
+                    //获得IPowerManager.Stub类\r
+                    Class cStub = Class\r
+                            .forName("android.os.IPowerManager$Stub");\r
+                    //获得asInterface方法\r
+                    Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);\r
+                    //调用asInterface方法获取IPowerManager对象\r
+                    Object oIPowerManager = asInterface.invoke(null, oRemoteService);\r
+                    //获得shutdown()方法\r
+                    Method shutdown = oIPowerManager.getClass().getMethod("shutdown",boolean.class,boolean.class);\r
+                    //调用shutdown()方法\r
+                    shutdown.invoke(oIPowerManager,false,true);\r
+\r
+                } catch (Exception e) {\r
+                    Log.e(TAG, e.toString(), e);\r
                 }\r
             }\r
         });\r