android 实现关机
2016-12-23 13:44
483 查看
android6.0以上版本,PowerManager有增加shutdown接口,直接调用即可。(android6.0与android7.0函数参数有差异)
android5.1以下版本:
转载至:http://www.cnblogs.com/snake-hand/p/3141014.html start
开始从网上搜索,通过发action的方式实现,不过一直没有成功。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SHUTDOWN);
sendBroadcast(intent);
加权限
<uses-permission android:name="android.permission.SHUTDOWN" tools:ignore="ProtectedPermissions" />
若有成功的同学,希望留言相告,谢谢。
这里介绍我自己的方法。
1. power服务实现了关机功能
framework/base/services/java/com/android/server/power/PowerManagerService.java
/**
* Shuts down the device.
*
* @param confirm If true, shows a shutdown confirmation dialog.
* @param wait If true, this call waits for the shutdown to complete and does not return.
*/
@Override // Binder call
public void shutdown(boolean confirm, boolean wait) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
final long ident = Binder.clearCallingIdentity();
try {
shutdownOrRebootInternal(true, confirm, null, wait);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
2. PowerManager提供了reboot等接口,没有提供shutdown接口。
若是重启,实现就很简单:
PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
pm.reboot();
但是shutdown没有实现,PowerManager的实现通过IPowerManager来调用Power服务的接口。
IPowerManager是AIDL文件自动生成的类,便于远程通信。IPowerManage.aidl文件目录framework/base/core/java/android/os/IPowerManage.aidl
3. IPowerManager实现了shutdown接口,这里只要获得Power服务的IBinder,通过反射调用shutdown方法就能实现关机功能。
ServiceManager管理着系统的服务程序,它保存着所有服务的IBinder,通过服务名就能获取到这个服务的IBinder。
而ServiceManager这个类也是HIDE的,也需要反射进行调用。代码实
96db
现:
android5.1以下版本:
转载至:http://www.cnblogs.com/snake-hand/p/3141014.html start
开始从网上搜索,通过发action的方式实现,不过一直没有成功。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SHUTDOWN);
sendBroadcast(intent);
加权限
<uses-permission android:name="android.permission.SHUTDOWN" tools:ignore="ProtectedPermissions" />
若有成功的同学,希望留言相告,谢谢。
这里介绍我自己的方法。
1. power服务实现了关机功能
framework/base/services/java/com/android/server/power/PowerManagerService.java
/**
* Shuts down the device.
*
* @param confirm If true, shows a shutdown confirmation dialog.
* @param wait If true, this call waits for the shutdown to complete and does not return.
*/
@Override // Binder call
public void shutdown(boolean confirm, boolean wait) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
final long ident = Binder.clearCallingIdentity();
try {
shutdownOrRebootInternal(true, confirm, null, wait);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
2. PowerManager提供了reboot等接口,没有提供shutdown接口。
若是重启,实现就很简单:
PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
pm.reboot();
但是shutdown没有实现,PowerManager的实现通过IPowerManager来调用Power服务的接口。
IPowerManager是AIDL文件自动生成的类,便于远程通信。IPowerManage.aidl文件目录framework/base/core/java/android/os/IPowerManage.aidl
3. IPowerManager实现了shutdown接口,这里只要获得Power服务的IBinder,通过反射调用shutdown方法就能实现关机功能。
ServiceManager管理着系统的服务程序,它保存着所有服务的IBinder,通过服务名就能获取到这个服务的IBinder。
而ServiceManager这个类也是HIDE的,也需要反射进行调用。代码实
96db
现:
而ServiceManager这个类也是HIDE的,也需要反射进行调用。
try { //获得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); }
相关文章推荐
- Android中的View
- Android 离线语音(讯飞语音)封装工具类
- Android NDK 编译出现 string:No such file or directory
- 关于Android中使用SVG特性的探索与总结
- Bmob后端云短信验证最新版Android studio集成登录演示
- Android四种点击事件和五中存储方式
- Android EditText 让编辑框只能输入汉字
- 自定义Log日志
- Android应用开发之图片(Bitmap)压缩(三)---------采样率压缩
- 隐式Intent 使用的之一
- Couldn't load XXX indLibrary returned null
- Android 真机调试
- Android bitmap 释放 解决Error create Hprof file问题
- QGIS 的安卓应用
- Android自定义View之常用工具源码分析
- Android Studio系列教程四--Gradle基础(转载)
- Android Studio系列教程六--Gradle多渠道打包(转载)
- Android的新虚拟ART与原虚拟机DVM的区别
- Android studio中调出代码快捷键,快速try catch包括代码块 if else
- Android中的Intent