Note that there are some explanatory texts on larger screens.

plurals
  1. POAllow notification to be cancelled after calling stopForeground(false)
    text
    copied!<p>I have a Media service that uses startForeground() to show a notification when playback starts. It has pause/stop buttons when playing, play/stop buttons while paused.</p> <pre><code>NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); // setup... Notification n = mBuilder.build(); if (state == State.Playing) { startForeground(mId, n); } else { stopForeground(false); mNotificationManager.notify(mId, n); } </code></pre> <p>The problem here is when I show/update the notification in it's paused state, you should be allowed to remove it. <code>mBuilder.setOngoing(false)</code> appears to have no effect as the previous <code>startForeground</code> overrides it.</p> <p>Calling <code>stopForeground(true);</code> with the same code works as expected, but the Notification flashes as it is destroyed and recreated. Is there a way to "update" the notification created from startForeground to allow it to be removed after calling stop?</p> <p><strong>Edit:</strong> As requested, here's the full code creating the notification. createNotification is called whenever the service is played or paused.</p> <pre><code>private void createNotification() { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("No Agenda") .setContentText("Live stream"); if (android.os.Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) { if (state == State.Playing) { Intent pauseIntent = new Intent(this, MusicService.class); pauseIntent.setAction(ACTION_PAUSE); PendingIntent pausePendingIntent = PendingIntent.getService(MusicService.this, 0, pauseIntent, 0); mBuilder.addAction(R.drawable.pause, "Pause", pausePendingIntent); //mBuilder.setOngoing(true); } else if (state == State.Paused) { Intent pauseIntent = new Intent(this, MusicService.class); pauseIntent.setAction(ACTION_PAUSE); PendingIntent pausePendingIntent = PendingIntent.getService(MusicService.this, 0, pauseIntent, 0); mBuilder.addAction(R.drawable.play, "Play", pausePendingIntent); mBuilder.setOngoing(false); } Intent stopIntent = new Intent(this, MusicService.class); stopIntent.setAction(ACTION_STOP); PendingIntent stopPendingIntent = PendingIntent.getService(MusicService.this, 0, stopIntent, 0); setNotificationPendingIntent(mBuilder); mBuilder.addAction(R.drawable.stop, "Stop", stopPendingIntent); } else { Intent resultIntent = new Intent(this, MainActivity.class); PendingIntent intent = PendingIntent.getActivity(this, 0, resultIntent, 0); mBuilder.setContentIntent(intent); } Notification n = mBuilder.build(); if (state == State.Playing) { startForeground(mId, n); } else { stopForeground(true); mNotificationManager.notify(mId, n); } } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void setNotificationPendingIntent(NotificationCompat.Builder mBuilder) { Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); } </code></pre> <p><strong>Follow-up Edit:</strong></p> <p>One of the comments below mentioned that the answer may be "fragile", and as of the release of Android 4.3, the behavior behind <code>startForeground</code> has changed. startForeground will force your application to show a notification while in the foreground, and the method should just be called with the notification to show. I haven't tested, but the accepted answer may no longer work as intended.</p> <p>In terms of stopping the flashing when calling <code>stopForeground</code>, I don't think it's worth fighting the framework for.</p> <p>There's some <a href="http://www.androidpolice.com/2013/07/29/android-engineer-explains-why-some-apps-suddenly-have-persistent-notifications-after-you-upgrade-to-android-4-3/" rel="noreferrer">additional information on the Android 4.3 notification change here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload