開始從網上搜尋,通過發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的,也需要反射進行調用。
代碼實現:
[java] view plaincopyprint?
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);
}
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);
}