//关机
systemOperation("shutdown")
//重启
systemOperation("reboot")
/**
* 系统操作
*/
private fun systemOperation(name: String) {
try {
//获得ServiceManager类
val ServiceManager = Class.forName("android.os.ServiceManager")
//获得ServiceManager的getService方法
val getService = ServiceManager.getMethod("getService", String::class.java)
//调用getService获取RemoteService
val oRemoteService = getService.invoke(null, Context.POWER_SERVICE)
//获得IPowerManager.Stub类
val cStub = Class.forName("android.os.IPowerManager\$Stub")
//获得asInterface方法
val asInterface = cStub.getMethod("asInterface", IBinder::class.java)
//调用asInterface方法获取IPowerManager对象
val oIPowerManager = asInterface.invoke(null, oRemoteService)
for (method in oIPowerManager.javaClass.methods) {
Log.i("SystemUtil", "方法名:" + method.name)
//获取本方法所有参数类型,存入数组
val getTypeParameters = method.parameterTypes
if (getTypeParameters.isEmpty()) {
Log.i("SystemUtil", "此方法无参数")
}
for (class1 in getTypeParameters) {
val parameterName = class1.name
Log.i("SystemUtil", "参数类型:$parameterName")
}
Log.i("SystemUtil", "****************************")
}
val method = oIPowerManager.javaClass.getMethod(
name,
Boolean::class.java,
String::class.java,
Boolean::class.java
)
method.invoke(oIPowerManager, false, null, false)
} catch (e: ClassNotFoundException) {
e.printStackTrace()
} catch (e: NoSuchMethodException) {
e.printStackTrace()
} catch (e: InvocationTargetException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
}
}