ARMAGEDDON ARMAGEDDON - 4 months ago 11
Android Question

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

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.

Answer

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