2011-08-24 76 views
3

我注意到,一些正在運行的服務在系統設置中的應用程序具有在Running services的詳細信息頁面Settings而非Stop按鈕。我想設置自己的服務來這樣工作。如何在Android中爲我的服務設置「控制面板」?

挖掘到的設置應用程序的源代碼後,我發現了一個線索:

ActivityManager.getRunningServiceControlPanel()

返回的PendingIntent你就可以開始顯示給定的正在運行的服務的控制面板。如果該服務沒有控制面板,則返回null。

我的問題是:如何爲我自己的服務設置control panel

+1

我相當肯定這是隻提供給那些固件的一部分應用。 – CommonsWare

+0

輸入法也有這個。 –

+0

此外,可能有一個「控制面板」entiry應用程序? –

回答

0

好吧,讓我們來完成這個。我認爲這是不可能的。

3

如果有人好奇,我發現這個功能背後的代碼。當且僅當調用者以SYSTEM身份運行時,服務的描述和配置意圖可以在服務綁定期間設置。

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/com/android/server/am/ActivityManagerService.java#11651

 int clientLabel = 0; 
     PendingIntent clientIntent = null; 

     if (callerApp.info.uid == Process.SYSTEM_UID) { 
      // Hacky kind of thing -- allow system stuff to tell us 
      // what they are, so we can report this elsewhere for 
      // others to know why certain services are running. 
      try { 
       clientIntent = (PendingIntent)service.getParcelableExtra(
         Intent.EXTRA_CLIENT_INTENT); 
      } catch (RuntimeException e) { 
      } 
      if (clientIntent != null) { 
       clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); 
       if (clientLabel != 0) { 
        // There are no useful extras in the intent, trash them. 
        // System code calling with this stuff just needs to know 
        // this will happen. 
        service = service.cloneFilter(); 
       } 
      } 
     } 

這個代碼在某些時候很感動,但仍然存在奇巧,持平。

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/com/android/server/am/ActiveServices.java#665

相關問題