Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add my app's icon into the status bar when my app is running?

Tags:

android

I tried to use the Notification.Builder and the Notification classes.

I tried using this code :

Notification notification = new Notification.Builder(this).build();
notification.icon = R.drawable.ic_launcher;
notification.notify();

but it seems useless.

I only want my app's icon to be added next to the battery icon,wifi icon and 3g icons.. Any way to do that? I appreciate your help.

like image 516
ARMAGEDDON Avatar asked Jun 02 '13 21:06

ARMAGEDDON


1 Answers

You have to call the method build() after you have finished describing your notification. Check out the Android reference for an example.

Basically, you have to change your code to the following:

Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context )
.setSmallIcon(R.drawable.ic_launcher);      

Intent intent = new Intent( context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, mID , intent, 0);
builder.setContentIntent(pIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notif = builder.build();
mNotificationManager.notify(mID, notif);

Note: this code will only allow to show your icon in the notification bar. If you want it to persist there, you will have to use FLAG_ONGOING_EVENT

like image 85
verybadalloc Avatar answered Sep 30 '22 13:09

verybadalloc