简体   繁体   中英

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.

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

You can add your app icon for status bar notification. Try this one

Notification notification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM