Categories: Android

Changing Service icon at Run time in Android

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.

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:minLevel="0" android:maxLevel="0" android:drawable="@drawable/default" />
<item android:minLevel="1" android:maxLevel="20" android:drawable="@drawable/yellow" />
<item android:minLevel="21" android:maxLevel="40" android:drawable="@drawable/green" />
<item android:minLevel="41" android:maxLevel="60" android:drawable="@drawable/red" />
/>
</level-list>

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

public static Notification notification;
public static NotificationManager mNotificationManager;

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

public void onCreate() {
super.onCreate();
notification = new Notification(R.drawable.notification_icon, "Kode Snippets"
, System.nanoTime());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "Kode Snippets Service", "Great! Service is running", pendingIntent);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.iconLevel = 0;
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(1, notification);

}

 

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

private Notification mainActivityService;

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.

public void updateUI(int value) {
//MainActivityService is your service class from which you're accessing your notification's static object.
mainActivityService = MainActivityService.notification;
mainActivityService.iconLevel = value;
MainActivityService.mNotificationManager.notify(1, mainActivityService);

}
}

 

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.

Aaqib Hussain

View Comments

Recent Posts

Things to know when moving to Germany

This article covers some important things you must know when you are considering a move…

3 years ago

Unit Testing in Android for Dummies

What is Unit Testing? In its simplest term, unit testing is testing a small piece…

4 years ago

Factory Design Pattern

In this article, you will learn about a type of Creational Design Pattern which is…

5 years ago

Creating Target specific Theme in iOS

In this tutorial, you will go through the use of targets to achieve two separate…

5 years ago

Facade Design Pattern

In this article, you will learn about a type of Structural Design Pattern which is…

5 years ago

Singleton Design Pattern

In this article you will learn about a type of Creational Design Pattern which is…

5 years ago