2011-03-21 60 views
5

做前臺服務的正確方法是什麼,我可以稍後綁定它? 我遵循Android API演示,其中包括如何創建前臺服務的示例。 沒有關於啓動服務在同一時間綁定到它的例子。可綁定的Android前臺服務

我想看到活動的音樂播放服務「綁定」到它的一個很好的例子。

有什麼?

我想要做的事,如:

  • 當一個程序是第一(第一意味着服務尚未啓動)開始我要開始前臺服務,做所有的工作。用戶界面(活動)就是當用戶按下home鍵的服務必須保持正常運行(在欄通知必須存在),以控制工作
  • 現在,如果在通知欄活動的通知用戶點擊應該開始和綁定到服務(或類似的東西,正確的方式)和用戶控制工作。
  • 如果活動是活躍和用戶按下後退按鈕活動應被銷燬,也服務應該被銷燬。

什麼方法我必須重寫來完成這項任務? Android的做法是什麼?

回答

-3

爲了完成任務,問我要做的唯一事情是追隨性的AndroidManifest.xml添加到我的活動定義

android:launchMode="singleTop" 

就是這樣。

問候

10

升級Froyo之前有setForeground(真)的服務,這是很容易,但也容易被濫用。

需要通知現在有startForeGround服務被激活(使用戶可以看到有一個foregroundservice運行)。

我做了這個類來控制它:

public class NotificationUpdater { 
    public static void turnOnForeground(Service srv,int notifID,NotificationManager mNotificationManager,Notification notif) { 
     try { 
      Method m = Service.class.getMethod("startForeground", new Class[] {int.class, Notification.class}); 
      m.invoke(srv, notifID, notif); 
     } catch (Exception e) { 
      srv.setForeground(true); 
      mNotificationManager.notify(notifID, notif); 
     } 
    } 

    public static void turnOffForeground(Service srv,int notifID,NotificationManager mNotificationManager) { 
     try { 
      Method m = Service.class.getMethod("stopForeground", new Class[] {boolean.class}); 
      m.invoke(srv, true); 
     } catch (Exception e) { 
      srv.setForeground(false); 
      mNotificationManager.cancel(notifID); 
     } 
    } 
} 

然後我的媒體播放器,此更新的通知 - 注意,前臺服務,而媒體播放和停止後,應留在只需要時,一個不好的做法。

private void updateNotification(){ 
    boolean playing = ((mFGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING) || 
         (mBGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING)); 
    if (playing) { 
     Notification notification = getNotification(); 
     NotificationUpdater.turnOnForeground(this,Globals.NOTIFICATION_ID_MP,mNotificationManager,notification); 
    } else { 
     NotificationUpdater.turnOffForeground(this,Globals.NOTIFICATION_ID_MP,mNotificationManager); 
    } 
} 

爲綁定 - 你只是綁定在你的活動以正常的方式調用onStart你只要把bindService電話,你會綁定到任何服務(這並不重要天氣它的前景還是不)

MediaPlayerService mpService=null; 
@Override 
protected void onEWCreate(Bundle savedInstanceState) { 
    Intent intent = new Intent(this, MediaPlayerService.class); 
    startService(intent); 
} 

@Override 
protected void onStart() { 
    // assume startService has been called already 
    if (mpService==null) { 
     Intent intentBind = new Intent(this, MediaPlayerService.class); 
     bindService(intentBind, mConnection, 0); 
    } 
} 

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     mpService = ((MediaPlayerService.MediaBinder)service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     mpService = null; 
    } 
}; 
+0

什麼是「onEWCreate」,是你的手寫錯誤或我的小知識? – pengwang 2011-03-23 05:41:00

+0

oppps對不起,它顯示onCreate - 這是我自己的異常報告封裝。 – siliconeagle 2011-03-23 21:12:56

+0

我發現了API Demos演示的奇怪行爲。如果您轉到API演示(API版本7)並轉到應用程序 - >服務 - >本地服務控制器,點擊啓動服務。現在通知在notif。酒吧正在運行。現在按HOME按鈕,並通知。欄並按下服務通知(Sample Local Service)。現在!你必須按回三次才能返回。爲什麼在世界上?!!? – zmeda 2011-04-04 19:49:32