2012-09-13 57 views
6

我創建了一個應用程序,它在Service.onStartCommand()方法執行時顯示通知,並在調用Service.onDestroy()時隱藏通知。它的工作正常調用startService()和stopService()。當Android操作系統銷燬服務時刪除通知

服務代碼:

@Override 
public IBinder onBind(Intent intent) 
{ 
    return null; 
} 
@Override 
public void onCreate() 
{ 
    super.onCreate(); 
    nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    //Some code here 
    showNotification(); 
    return START_NOT_STICKY; 
} 

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    nm.cancel(R.string.service_started); 
} 

問題:當我的服務是由Android操作系統被破壞()(在不用時,稱爲onDestroy()),那麼通知不會被刪除。那麼如何在Android操作系統本身銷燬服務時刪除通知?

回答

0

最有可能你沒有停止你的服務

+0

我米停止服務,這是工作正常的情況下。但是當我的服務被android操作系統()(可能是內存不足或其他東西)破壞時,那麼如何刪除通知 – user1041858

+0

@ user1041858爲什麼你認爲在這種情況下不會調用onDestroy? –

+0

當android系統自動銷燬服務時,我有這個問題。 – user1041858

0

重寫onStop方法的生命週期正常工作。

1

你應該使用這個你想停止服務的地方,這將關閉你的通知和最終服務。

stopService(new Intent(MainActivity.this,MyService.class)); 
相關問題