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 notification bar. I wanted a persistent icon that when you pull down the notification bar it shows that the service is running and you can click it to open the app to shut the service down.
Can anyone point me to resources or tutorial on how to accomplish this any APK is fine but would like work with 4.0 and greater.
Thanks.
What Are Android Status Bar Icons? Android Status Bar icons are notifications in the graphical user interface (GUI) from apps running on your device. These notifications can contain text, graphics, and even controls.
First on the list is the “Status Bar” option. Jump in there. These settings are pretty straightforward—just turn a toggle off to hide that icon. The changes take effect in real-time, so you can see how you feel about them on the fly.
First, press-and-hold on the persistent notification you want to remove. Another option is to swipe the notification left or right, and then tap on the cogwheel icon shown next to it. Next, tap on the switch next to Permanent to disable it, and then press Save.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With