54

enter image description here

Hello, How do i create the permanent notification like the first one for Battery Indicator?

Rohith Nandakumar
  • 11,367
  • 11
  • 50
  • 60

4 Answers4

91

In case you are using NotificationCompat.Builder , you can use :

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
mBuilder.setOngoing(true); //this will make ongoing notification
Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59
89

Assign Notification.FLAG_ONGOING_EVENT flag to your Notification.

Sample code:

yourNotification.flags = Notification.FLAG_ONGOING_EVENT;
// Notify...

If you aren't familiar with the Notification API, read Creating Status Bar Notifications on Android developers website.

Wroclai
  • 26,835
  • 7
  • 76
  • 67
50

It is actually recommended to bitwise-or notification flags, rather than set the flags directly. This allows you to set multiple flags at once.

For example:

notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

will set both flags at once, whereas:

notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags = Notification.FLAG_SHOW_LIGHTS;

will only set the FLAG_SHOW_LIGHTS flag.

joebobfrank
  • 957
  • 9
  • 13
5
public static final int FLAG_ONGOING_EVENT

Since: API Level 1
Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call.

public static final int FLAG_FOREGROUND_SERVICE

Since: API Level 5 Bit to be bitwise-ored into the flags field that should be set if this notification represents a currently running service.

Aleadam
  • 40,203
  • 9
  • 86
  • 108