1
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
    builder.setContentTitle(description.getTitle()) 
       .setAutoCancel(false); 
    NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notificationCompat=builder.build(); 
    notificationManager.notify(MY_ID,notificationCompat); 
    startForeground(MY_ID,notificationCompat); 

使用上述代碼在後臺運行服務。但是,當活動關閉,服務調用的onDestroy如何在NotificationCompat.Builder中使用startForeground?

+0

「不工作」?什麼不起作用? –

+0

當顯示屏關閉服務電話onDestroy –

回答

0

您可以從IntentService擴展您的服務,所以它的工作與單獨的線程和setOngoing忠實於您的通知

builder.setOngoing(true) 

該用戶後無法手動刪除通知,並它支持在工作狀態下的服務。 而當活動結束時,您的服務將繼續有效。

+0

它的媒體播放器服務,在後臺播放 –

+0

如果我理解正確,您的意思是您的服務無法從IntentService擴展,因爲媒體播放器服務從服務(https://開發人員.android.com/guide/topics/media/mediaplayer.html#mpandservices)。因此,嘗試將setOngoing(true)設置爲您的通知,而無需從IntentService進行擴展。 –

相關問題