• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to keep notification message showing after a service is started until it is stopped?

Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am able to get the notification message to pop up for a split second when I click a button to start a service. Then it disappears (dropping down out of sight) when the service is still running. I need to keep the message showing until I click another button to stop the service. Shouldn't it be shown persistently? Any learning resource on this particular topic? Use a thread? Thanks in advance! Here's the code:

public class MusicPlayerService extends Service {

MediaPlayer player;
private NotificationManager mNM;

public IBinder onBind(Intent intent) {
return null;

// Called by the system when the service is first created.
public void onCreate() {

mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
player = MediaPlayer.create(this, R.raw.happy_birthday_free);

// Display a notification about us starting. We put an icon in the status bar.


// Called by the system every time a client explicitly starts the service
// by calling startService(Intent), providing the arguments it supplied and a
// unique integer token representing the start request.
public int onStartCommand(Intent intent, int flags, int startId) {

// We want this service to continue running until it is explicitly
// stopped, so return sticky.

// Called by the system to notify a Service that it is no longer used and is being
// removed. The service should clean up an resources it holds (threads, registered
// receivers, etc) at this point. Upon return, there will be no more calls in to
// this Service object and it is effectively dead.
public void onDestroy() {



* Show a notification while this service is running.
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.local_service_started);

// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.icon, text,

// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ServiceLauncher.class), 0);

// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label),
text, contentIntent);

mNM.notify(R.string.local_service_started, notification);
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !

You can set notification icon to be always on top bar.

Just simple - set the flag - notification.flags |= Notification.FLAG_NO_CLEAR; (notification is Notification class object)

Of course you can remove notification icon by invoking cancel(notification id) method

Kev Lee
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
notification.flags |= Notification.FLAG_NO_CLEAR should work. But it does not work in my code even after some modifications. But I will continue to keep trying it. Thanks!.
    Bookmark Topic Watch Topic
  • New Topic