• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kev Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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;


@Override
public IBinder onBind(Intent intent) {
return null;
}



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

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

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

}

// 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.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

player.start();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_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.
@Override
public void onDestroy() {

mNM.cancel(R.string.local_service_started);
Toast.LENGTH_SHORT).show();
player.stop();

}

/**
* 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,
System.currentTimeMillis());

// 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);
}}
 
Przemek Boryka
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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

Bye!
 
Kev Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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!.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic