android 實現代碼關機

來源:互聯網
上載者:User

開始從網上搜尋,通過發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的,也需要反射進行調用。

代碼實現:

       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);               }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.