2016-10-10 65 views
0

我正在開發一個必須始終處於活動狀態的應用程序,它將用於專用於應用程序的設備。當應用程序從最近的應用程序窗口中被刪除時取消通知

我讀過,不讓應用程序被操作系統殺死我需要設置一個持續的通知,我也應用了部分喚醒鎖。

的問題是該通知不會消失,如果應用程序被殺死了,我把這個在我的活動

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
Intent intent = new Intent(getApplicationContext(), PlayerActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 8000, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
Notification.Builder builder = new Notification.Builder(getApplicationContext()) 
     .setContentTitle("Title") 
     .setContentText("Description") 
     .setContentIntent(pendingIntent) 
     .setSmallIcon(R.mipmap.ic_playlist_play_white_36dp) 
     .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon)) 
     .setOngoing(true); 

notificationManager.notify(8000, builder.build()); 

onCreate方法上onDestroy方法我稱之爲

notificationManager.cancelAll(); 
super.onDestroy(); 

通知永遠不會被刪除,我不得不卸載應用程序被殺死,有沒有辦法刪除它時,應用程序沒有加載?

編輯:我針對API 19

+0

不幸的onDestroy方法不這樣做一個安全的地方和Android可能不會調用一些這種方法的情況。 可能你必須創建一個服務來處理這種情況,如果有必要的話! –

回答

1

你應該使用Foreground Service,這將防止操作系統殺害您的應用程序(設置的通知正在進行本身不會阻止)。服務還提供了一種手段來檢測應用程序通過onTaskRemoved()被刷掉。

首先,你需要在你的清單申報服務:

<service android:name="com.your.app.NotificationService"/> 

然後從活動啓動服務:

Intent intent = new Intent(this, NotificationService.class); 
intent.putExtra("key", "value"); // if you have extras 
this.startService(intent); 

將服務設置爲前臺服務的onStartCommand()

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent pending = new Intent(getApplicationContext(), PlayerActivity.class); 
    pending.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 8000, pending, PendingIntent.FLAG_CANCEL_CURRENT); 
    Notification.Builder builder = new Notification.Builder(getApplicationContext()) 
      .setContentTitle("Title") 
      .setContentText("Description") 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.mipmap.ic_playlist_play_white_36dp) 
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon)) 
      .setOngoing(true); 

    startForeground(8000, builder.build()); 

    return (START_NOT_STICKY); 
} 

最後處理onTaskRemoved()onDestroy()

@Override 
public void onTaskRemoved(Intent rootIntent) { 
    stopSelf(); 
} 

@Override 
public void onDestroy() { 
    stopForeground(true); // 'true' removes notification too 
} 

N.B.撥打stopSelf()將銷燬該服務,然後stopForeground()將自動刪除該通知。

最後,如果你想從一個活動手動停止服務,你可以簡單地這樣做:

Intent intent = new Intent(this, NotificationService.class); 
this.stopService(intent); 
+0

這是我需要的;部分喚醒鎖不足以保持活動活動?,該應用程序的主要目標是播放來自遠程http服務器的音頻文件,只需在屏幕關閉時永久保持活動狀態 –

+1

請參閱:http:// stackoverflow。 com/questions/9104622/android-foreground-service-vs-wakelock ...部分喚醒鎖確保CPU喚醒來執行你的任務,並且前臺服務確保你的應用的優先級不會在低內存中死掉 –

相關問題