There can be times when you might need the ability in your app to change your service icon according to your wish.

Let’s say you have an app that is related to Geo Fencing and you want to set the icon to green when user is in the required radius and to red when he is not, or something like WiFi signals icon; as much as you’re near to a router, the icon with full signals appear and vice versa.

To achieve this you must have a function in your Main Activity or the Activity that you are running as your service to have a function which is updating at some instance.

I’m going to show you a simple way to implement that.
At first you need to create a XML file that will be holding all the icons for your service. you would also need to give every icon a threshold level; i.e on exceeding which you want the icon to change.

After things being set. Open your Service class and create two static variables.

and in onCreate of your service. Create a notification with reference to the xml you created above.

 

With notification being created, open your MainActivity and create a variable

Then go to the function which updates every second or at some point. Let’s consider I have a function that updateUI which returns me some integer value every time something changes.

 

What actually happening here is that whenever your function will be called it will set the notification.iconLevel to the current value whatever that would be at that time and pass it to notificationManager that will remove the current icon and set the new one and so on.

If you guys have any questions, please leave a comment.

Have a nice day.

Share:

administrator

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *