I am creating an android service which will start running after the device boot process completed. In my service i am creating a task. This task will be started or stopped at any time depending on some conditions. My intention is whenever i started my task, i want to display an icon in status bar to know that my task is running like bluetooth icon will be displayed when it is turned ON.
Asked
Active
Viewed 1.3k times
5
-
Possible duplicate of [How to show an icon in the status bar when application is running, including in the background?](http://stackoverflow.com/questions/3973208/how-to-show-an-icon-in-the-status-bar-when-application-is-running-including-in) – Ciro Santilli OurBigBook.com Feb 12 '16 at 19:22
2 Answers
7
You need a Notification. Code is talking :)
In your Service:
private NotificationManager mNM;
private int NOTIFICATION = 10002; //Any unique number for this notification
To show the notification:
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.status_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, MainActivity.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);
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
and to hide it, you simply do this:
mNM.cancel(NOTIFICATION); //The same unique notification number.
Here are some clarifications:
R.drawable.status_icon
: Notification iconR.string.local_service_started
: Notification titleR.string.local_service_label
: Notification latest info (sub-title)MainActivity.class
: The Activity that will be launched when the user click the notification

iTurki
- 16,292
- 20
- 87
- 132
-
I think notification is different from status bar icon. I mentioned like Bluetooth icon displayed on status bar. I think your answer is not suitable for my requirements. Anyhow thanks for responding to my question. – Yugandhar Babu Jan 24 '12 at 09:41
-
You mean like an icon that the user will never interact with? Like the 3G icon, sync and the headphone? – iTurki Jan 24 '12 at 09:44
-
1There is a class in Android source code called [StatusBarPolicy.java](http://hi-android.info/src/com/android/server/status/StatusBarPolicy.java.html). In the constructor, every icon that may appear in the status bar is initialized and set to visible or invisible (as needed). You may refer to [this thread](http://stackoverflow.com/questions/6922878/how-to-remove-the-battery-icon-in-android-status-bar) for more info. – iTurki Jan 24 '12 at 09:57
-
And to answer your question, I don't think it is possible for apps other than system apps. – iTurki Jan 24 '12 at 09:58
0
You can use a Custom title bar like Custom title with image
And Check the preference setting that your service is enable or disable and set custom title. but before doing this please Read : http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Community
- 1
- 1

deepak Sharma
- 1,641
- 10
- 23
-
Custom title is different from status bar icon. Actually in my app no activity, its a service launched automatically after device booted. Is it possible to use custom title without activity in app ? Anyhow thanks for your answer. – Yugandhar Babu Jan 12 '12 at 13:16
-
yes, its possible to start service without any activity at all... just start your service as Foreground. You'll be providing a notification object in order to start service – waqaslam Jan 12 '12 at 13:25