简体   繁体   中英

How to change the status bar icon while service is running

I want to change the notification smallIcon in the status bar while a foreground service is running, depending on the status the service gathers, namely "mute" or "unmute".

What is needed to display alternate smallIcons from the res.drawable resouce?

In the initialize method of the service class, I currently set the mute icon as follows, but I don't know how to change it after the service was started:

NotificationCompat.Builder builder = new NotificationCompat.Builder(
        this, NOTE_CHANNEL_ID)
        .setSmallIcon(R.drawable.mute_icon)
        .setContentTitle("Calm: Running")
        .setContentText(this.getString(R.string.calm_close_txt))
        .setOngoing(true)
        .setContentIntent(stopIntent);

startForeground(NOTIFICATION_ID, builder.build());

The workaround is to just create a new notification with a new icon, so it'll replace the old one.

EDIT: Here's a sample code for creating a new notification every time a button is clicked with a sample logic based on a boolean variable called indicator .

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Create the notification builder object
        NotificationCompat.Builder builder = new NotificationCompat.Builder(v.getContext(), NOTE_CHANNEL_ID)
                .setSmallIcon(indicator ? R.drawable.icon1 : R.drawable.icon2)   //TODO: change this line with your logic
                .setContentTitle("Your notification title here")
                .setContentText("Your notificiation text here")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
//                        .setContentIntent(pendingIntent)    //pendingIntent to fire when the user taps on it
                .setAutoCancel(true);   //autoremove the notificatin when the user taps on it

        //show the notification constructed above
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(v.getContext());
        notificationManager.notify(NOTIFICATION_ID, builder.build());

        indicator = !indicator; //switch icon indicator (just for demonstration)
    }
});

You only need to hold a reference to the NotificationCompat.Builder . Then use the notify(int id, Notification notification) method of NotificationManagerCompat .

Example:

NotificationCompat.Builder notificationBuilder;

private void startNotif(){
    notificationBuilder = new NotificationCompat.Builder(
        this, NOTE_CHANNEL_ID)
        .setSmallIcon(R.drawable.mute_icon)
        .setContentTitle("Calm: Running")
        .setContentText(this.getString(R.string.calm_close_txt))
        .setOngoing(true)
        .setContentIntent(stopIntent);
    
    startForeground(NOTIFICATION_ID, notificationBuilder.build());
}

private void changeIcon(int resID, Context context){
    notificationBuilder.setSmallIcon(resID);
    NotificationManagerCompat notificationManagerCompat =
                NotificationManagerCompat.from(context);
    notificationManagerCompat.notify(NOTIFICATION_ID, notificationBuilder.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