I am trying to figure out how to show that my service is running with a persistent notification icon. Many examples I\'ve found only send a dismissable message to the notifi
it should be the same as a dismissable message except you change the Flag.
Notification.FLAG_ONGOING_EVENT
instead of
Notification.FLAG_AUTO_CANCEL
When a notification is clicked the intent you send is run, so you make sure that Activity does whatever task you want.
private void showRecordingNotification(){
Notification not = new Notification(R.drawable.icon, "Application started", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, main.class), Notification.FLAG_ONGOING_EVENT);
not.flags = Notification.FLAG_ONGOING_EVENT;
not.setLatestEventInfo(this, "Application Name", "Application Description", contentIntent);
mNotificationManager.notify(1, not);
}
I know this is an old question, but it was first on a google results page so I'll add information to help others.
The trick is to add .setOngoing to your NotificationCompat.Builder
A button that opens the app and shuts the service down requires a PendingIntent
This example shows a persistent notification with a close button that exits the app.
MyService
:
private static final int NOTIFICATION = 1;
public static final String CLOSE_ACTION = "close";
@Nullable
private NotificationManager mNotificationManager = null;
private final NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this);
private void setupNotifications() { //called in onCreate()
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
PendingIntent pendingCloseIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
.setAction(CLOSE_ACTION),
0);
mNotificationBuilder
.setSmallIcon(R.drawable.ic_notification)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(getText(R.string.app_name))
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_menu_close_clear_cancel,
getString(R.string.action_exit), pendingCloseIntent)
.setOngoing(true);
}
private void showNotification() {
mNotificationBuilder
.setTicker(getText(R.string.service_connected))
.setContentText(getText(R.string.service_connected));
if (mNotificationManager != null) {
mNotificationManager.notify(NOTIFICATION, mNotificationBuilder.build());
}
}
MainActivity
must handle the close intents.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String action = intent.getAction();
if (action == null) {
return;
}
switch (action) {
case MyService.CLOSE_ACTION:
exit();
break;
}
}
private void exit() {
stopService(new Intent(this, MyService.class));
finish();
}
AnotherActivity
must be finish and send an exit intent to MainActivity
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String action = intent.getAction();
if (action == null) {
return;
}
switch (action) {
case MyService.CLOSE_ACTION:
exit();
break;
}
}
/**
* Stops started services and exits the application.
*/
private void exit() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setAction(Stn1110Service.CLOSE_ACTION);
startActivity(intent);
}
Can anyone point me to resources or tutorial
http://developer.android.com/training/notify-user/build-notification.html