2012-10-13 22 views
7

我目前正在使用服務啓動時出現在通知欄中的通知創建前臺服務。如果服務停止,通知將被解除。我的問題是,有沒有辦法在「清除所有通知」或解除通知(刷卡)時停止服務?如何使用通知取消前臺服務(刷卡解除)或清除所有通知?

更新以包括執行通知:

public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    Log.d(CLASS, "onStartCommand service started."); 

    if (getString(R.string.service_start_action) == intent.getAction()) 
    { 
     Intent intentForeground = new Intent(this, ServiceMain.class) 
      .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);  
     PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0);  
     Notification notification; 
     Notification.Builder builder = new Notification.Builder(getApplicationContext()) 
      .setSmallIcon(android.R.drawable.btn_star) 
      .setTicker("Service started...") 
      .setContentIntent(pendIntent) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setOnlyAlertOnce(true) 
      .setOngoing(false); 
     notification = builder.build(); 
     notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 

     startForeground(SERV_NOTIFY, notification); 
     player.start(); 
    } 
    else 
    { 
     Log.d(CLASS, "onStartCommand unable to identify acition."); 
    } 

    return START_STICKY;   
} 
+0

看看這個來自Google的示例:https://github.com/googlesamples/android-ActiveNotifications。它顯示瞭如何聆聽被駁回的通知以及整個通知組。 –

+0

據我所知,如果不創建通知,您無法「創建」前臺服務。 Android需要這樣做,因爲他們不希望你在用戶不知道的情況下在場景後面運行服務。當您的活動被銷燬並且您的服務在前臺運行時,請刪除通知以通知您,您不能僅僅取消它。取消通知只能通過調用'.stopForeground(true)'取消前臺服務本身,布爾參數指示是否要隨通知一起停止前臺。 –

回答

0

還有就是你可以在你創建的通知設置,但我以爲前臺服務通知的dismissIntent不能被清除?

+0

有「dismissintent」? API規範在哪裏? –

+0

@IgorGanapolsky NotificationCompat.Builder#setDeleteIntent(...) – Dogcat

+0

@Dogcat我在api的任何地方都看不到** dismissIntent **。 –

4

無法清除預知服務的通知。唯一的辦法就是停止服務。

所以我相信你想解決的問題,永遠不會發生。

3

我的問題是,有什麼辦法可以在「清除所有通知」或關閉通知(刷卡)時停止服務?

假設您的Notification設置爲允許清除,則應在清除時調用the deleteIntent。您可以將其設置爲getBroadcast()PendingIntent,指向清單註冊的BroadcastReceiver,它調用stopService()

+0

謝謝,這將是我最初的做法,但一旦服務的通知在通知欄中,就沒有「清除所有通知」按鈕,並且滑動解除功能似乎被禁用。 –

+0

@moncadad:編輯你的問題,發佈你正在創建'Notification'的代碼,並在這裏回覆評論,讓我知道你發佈了它。 – CommonsWare

+0

已更新爲包含通知實施。 –

6

用戶不允許刷掉正在進行的前臺服務生成的通知。

因此,stopForeground(false)第一,它允許用戶此後(至少在棒棒糖+)的通知被刷掉。對於Lollipop之前的版本,您可能必須登錄stopForeground(true),它會停止前臺並刪除通知,然後使用notificationManager.notify(yourNotificationID,yourNotificationObject)重新發出通知,以便您的通知可見但可以滑動。

至關重要的是,設置通知對象的刪除意圖,當用戶將其刪除時觸發。

(new NotificationCompat.builder(this).setDeleteIntent(deletePendingIntent)).build() 

其中deletePendingIntent是一樣的東西

Intent deleteIntent = new Intent(this, YourService.class); 
deleteIntent.putExtra(someKey, someIntValue); 
PendingIntent deletePendingIntent = PendingIntent.getService(this, 
someIntValue, 
deleteIntent, 
PendingIntent.FLAG_CANCEL_CURRENT); 

當用戶刷出去了,其額外的意圖傳遞到服務。在onStartCommand內處理交付的額外費用,即檢查intent != null,intent.getExtras() != null,然後從給定的someKey中提取額外費用中的值,並且如果它匹配someIntValue,則請致電stopSelf()

+0

我不得不使用startForeground(false),但我也必須從我的NotificationBuilder代碼中移除對.setOngoing(true)的調用 – caitoo0o

1

此代碼的工作對我來說:

this.stopForeground(false); 
mNotificationManager.cancel(NOTIFY_ID); 

,你應該設置onStartCommand你的返回值,STRAT_STICKY將重新啓動您的服務。