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);? ? ? ?
? ? ? ? }
五.使用ShutdownThread (尝试不成功,但想法觉得可行)
其实,使用,PowerManager 来关机或重启,PowerManager 接着还是会继续还是会调用到ShutdwonThread的相关接口。比如
PowerManager提供的reboot接口。在PowerManager中,reboot()实现代码如下 :
?public void reboot(String reason) {
? ? ? ? try {
? ? ? ? ? ? mService.reboot(false, reason, true);
? ? ? ? } catch (RemoteException e) {
? ? ? ? }
}
mService 是是IPowerManager类型的,前面我们说过,PowerManagerService是PowerManager的具体实现,而PowerManager调用PowerManagerService实现的接口,也正是通过PowerManager的实现是通过IPowerManager来调用Power服务的接口。所以,继续追踪PowerManagerService中的代码,可以知道其中就有reboot()的实现。
@Override // Binder call
? ? public void reboot(boolean confirm, String reason, boolean wait) {
? ? ? ? mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
? ? ? ? final long ident = Binder.clearCallingIdentity();
? ? ? ? try {
? ? ? ? ? ? shutdownOrRebootInternal(false, confirm, reason, wait);
? ? ? ? } finally {
? ? ? ? ? ? Binder.restoreCallingIdentity(ident);
? ? ? ? }
}
继续跟踪shutdownOrRebootInternal(false, confirm, reason, wait);
private void shutdownOrRebootInternal(final boolean shutdown, final boolean confirm,
? ? ? ? ? ? final String reason, boolean wait) {
? ? ? ? if (mHandler == null || !mSystemReady) {
? ? ? ? ? ? throw new IllegalStateException("Too early to call shutdown() or reboot()");
? ? ? ? }
? ? ? ? Runnable runnable = new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? synchronized (this) {
? ? ? ? ? ? ? ? ? ? if (shutdown) {
? ? ? ? ? ? ? ? ? ? ? ? ShutdownThread.shutdown(mContext, confirm);
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ShutdownThread.reboot(mContext, reason, confirm);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
我们就会知道,它最终是调用到了ShutdownThread.java里面的reboot()。从上面的代码中,我们还知道,ShutdownThread同时还提供了关机接口。
因此,要是可以利用ShutdownThread,那么,一切就会方便很多.
不过,Shutdown并没有提供公开API,网上有人说,可以,直接
Import com.android.server.pm.ShutdownThread.java
通过,执行Shutdown.shutdown() 或者Shutdown.reboot(), 就可以相应地关机或重启。
但是,我放在源码环境下,发现也没能编译通过,具体什么原因,也没再去深究,下次有时间,再继续看看。不过,我觉得这种做法也是可以,可能是我遗漏了某些东西,导致编译没通过。
?
?
?