2013-08-16 65 views
2

我正在嘗試編寫MediaPlayer附帶的服務。我有不同的活動訪問它,所以我認爲最好仔細閱讀一項服務。Android上的持續服務

它迄今爲止工作正常,我還添加了一個電話startForeground,如here所述。通知顯示出來。

但是,當我現在按設備上的主頁或後退按鈕時,服務將停止並調用onDestroy,並且通知圖標消失。當我回來時,服務似乎很好。

我停止音樂播放onDestroy,所以它當然停止。但是,即使用戶使用其他應用程序,我也希望保持通知和服務的連貫性。

編輯:我希望這是相關部分:

public class MediaPlayerService extends Service { 
    private static class PlayerMessageHandler extends Handler { 
     private final MediaPlayerService owner; 

     public PlayerMessageHandler(MediaPlayerService owner) { 
      this.owner = owner; 
     } 

     @Override 
     public void handleMessage(Message msg) { 
      // Handle 
     } 
    } 

    private static final int NOTIFICATION_ID = 13138; 

    private final Messenger messenger = new Messenger(new PlayerMessageHandler(
      this)); 
    private MediaPlayer player; 
    private Notification notification; 

    @Override 
    public IBinder onBind(Intent intent) { 
     startNotification(); 
     return messenger.getBinder(); 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     Log.v(TAG, "Media player service created."); 

     player = new AudiobookPlayer(this); 
     new Thread(seekerUpdate).start(); 
     isRunning = true; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.v(TAG, "Received start id " + startId + ": " + intent); 
     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.v(TAG, "Media player service destroyed."); 

     if (player.isPlaying()) { 
      player.pause(); 
     } 
     sendMessageToUI(MSG_PLAYER_HAS_PAUSED); 

     isRunning = false; 
    } 

    private void sendMessageToUI(int msg) { 
     Log.v(TAG, "Sending " + msg); 

     sendMessage(Message.obtain(null, msg)); 
    } 

    private void sendMessage(final Message message) { 
     // Send 
    } 

    private void startNotification() { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(
       this); 
     builder.setSmallIcon(R.drawable.notification); 
     builder.setContentTitle(getString(R.string.app_name)); 
     notification = builder.build(); 

     startForeground(NOTIFICATION_ID, notification); 
    } 
} 

EDIT2:從活動的方法,從here

@Override 
protected void onStart() { 
    super.onStart(); 
    // Bind to the service 
    bindService(new Intent(this, MediaPlayerService.class), 
      playerServiceConnection, Context.BIND_AUTO_CREATE); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    // Unbind from the service 
    if (bound) { 
     unbindService(playerServiceConnection); 
     bound = false; 
    } 
} 
+0

對不起,但你的問題是什麼? – Bex

+0

@Bex:問題在於,即使用戶在另一個應用程序中,我也可以使服務保持活躍狀態​​。 – Lanbo

+0

我想,你需要一個Wakelock來讓服務持續下去 – flx

回答

3

採取你應該讓你的服務sticky。事實上,這是個什麼tutorial uses

public class HelloService extends Service { 
... 

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     ... 

     // If we get killed, after returning from here, restart 
     return START_STICKY; 
    } 
... 
} 

編輯:從自認爲蒸騰的討論中,事實證明,我的懷疑是正確的,並提供了雨果的點上診斷。我想你現在需要在startNotification()中加builder.setOngoing(true);

+0

我已經這樣做了。 – Lanbo

+0

@LambdaDusk:這是......不尋常的。你能用你的代碼編輯問題嗎? –

+0

我希望我有所有的相關部分。 – Lanbo

4

http://developer.android.com/guide/components/services.html 束縛

服務提取的 「結合」,當一個應用程序組件通過 結合它調用bindService()。綁定服務提供客戶端服務器接口,該接口允許組件與服務交互,發送請求,獲得結果,甚至跨進程使用進程間通信(IPC)執行此操作。一個綁定服務只運行 另一個應用程序組件綁定到它。 多個組件可以 立即綁定到服務,但是當它們全部解除綁定時,服務 被銷燬。

您結合您的onStart活動和解除綁定onStop。當您按主頁或返回時,您最後一次前臺活動可能會調用onStop,從服務中解除最後一個活動並殺死它。

另一種解決方案是撥打startService,因此將調用onStartCommand,然後調用bindService來綁定活動。

+0

我這樣做了,現在服務似乎停滯不前,但通知消失。並重新打開應用程序並殺死並重新啓動它。 – Lanbo