android 9.0系统重启的代码详解

在做一些系统应用中,需要添加重启功能,查询一些资料,最后还是通过反射解决这个问题,代码如下:

Class<?> serviceManager = Class.forName("android.os.ServiceManager");
                Method getService = serviceManager.getMethod("getService", String.class);
                Object remoteService = getService.invoke(null, Context.POWER_SERVICE);
                Class<?> stub = Class.forName("android.os.IPowerManager$Stub");
                Method asInterface = stub.getMethod("asInterface", IBinder.class);
                Object powerManager = asInterface.invoke(null, remoteService);
                Method shutdown = powerManager.getClass().getDeclaredMethod("reboot",
                        boolean.class, String.class, boolean.class);
                shutdown.invoke(powerManager, false, "", true);

你可能感兴趣的:(android)