Frequent Custom Status Bar Notification is Evil on Android

The Story Behind

In my recent app, video converter for Android, I add a notification progress report on the status bar. So users can view the video conversion progress any time. I also did several other changes, and then start running the final test before release.

Then I found the app is much slower compared with previous version. I couldn’t think of why. I turned on Power Tutor app, and found that the system process is consuming a lot of CPU. Notification bar is called through NotificationManager, which a system service. I guess it’s because I’m updating the status bar progress every second.

In the end, I changed the status progress update interval and app works as normal. But just to demonstrate frequent custom status bar notification is really evil, I programmed a simple testing app does nothing but status bar notification update.

Why I Say it’s Evil

The key part of the code is as below (you can get the entire code at the end of the post),

SystemClock.sleep(1000);

CharSequence title = "Freq noti is evil: " + mCount;

CharSequence content = "Freq notification update takes too much CPU";

if (CUSTOM_NOTI) {

    noti.contentView.setTextViewText(R.id.status_text, title);

    noti.contentView.setProgressBar(R.id.status_progress, 100, mCount%100, false);

} else {

    Intent notiIntent = new Intent(context, StatusBarNotificationActivity.class);

    PendingIntent pi = PendingIntent.getService(context, 0, notiIntent, 0);

    noti.setLatestEventInfo(context, title, content, pi);

}

//nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//nm = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

nm.notify(STATUS_BAR_NOTIFICATION, noti);

The testing app supports both custom and default status notification. The custom status bar notification contains an icon, a text view and a progress bar. It updates the notification once every second. 

The Power Tutor CPU power consumption curve is as below for default status notification,

Figure 1. CPU Power Consumption with Frequent Default Status Bar Notification Update

The left one is before testing app starts, the right one is when testing app is running. There’s almost no difference.

The Power Tutor CPU power consumption curve is as below for custom status notification,

 

Figure 2. CPU Power Consumption with Frequent Custom Status Bar Notification

The first one is before testing app starts, the second one is when testing app is running, and the third one is after testing app is finished. Clearly frequent update of the custom notification status consumes lots of CPU resources.

I also tried without updating the progress bar (so only the title text of the notification is updated), the consumption is less, but still much higher than default one.

Figure 3. Custom Notification without Progress bar

You can download the testing app here or from my github. Note that you might want to run the testing app for a while to see the results. At some of my tests, the CPU consumption is low initially, but raise high after a while.

The testing is done on Nexus One device with Android 2.3, the android sdk used for development is 2.1 and 4.0.

Power Tutor–Android App Measures Power Consumption of App

Mobile computing and smart phones cannot be what we see today without powerful and energy efficient batteries. Mobile application developers, however, don’t get used to think of building energy efficient apps. Sometimes, it can affect the application a lot.

I recently found a tool, Power Tutor, that runs on Android devices to measure the power performance of the device. This post is about how Power Tutor works and how it can help to develop Android apps. It doesn’t cover every detail of the theory (one can refer to the research paper in reference 1 if interested.), but briefly describes how it works in general.

Component-Based Measurement

Power Tutor measures the energy consumption by components, including CPU, WIFI, 3G, Display etc. Each component has different states, and the power consumption rate at each state differ. Power Tutor includes a set of parameters for each state describing how fast power is consumed at the state. The parameters are obtained by their developers from offline experiments. Use these parameters, they construct a power measurement model to measure the energy. Below are screenshots of Power Tutor measurement for a Android phone.

Figure 1. Power Measurement for Android Phone

The parameters they use in current app is obtained from several HTC phones, so it works best for those models (refer to reference 1 for model details). The method Power Tutor developers/researchers proposed is able to obtain the parameters on other phones, but the app doesn’t provide such a function.

Power Consumption for Each App

Power Tutor has a “Application Viewer” function to view the power consumption for each app.

On Android, each app/process is considered as a separate user with its own UID. Under /proc/uid_stat/<UID>/ directory, lots of information are available about the app/process, including data transmitted, memory usage etc. Power Tutor map process id to UID, then based on the stats found under the <UID> folder to decide the states for each component. Then based on the power measurement model, Power Tutor computes the energy consumption.

Power Tutor thinks the energy consumption of each app is independent. In other words, Power Tutor assumes app A consumes the same amount of energy with or without app B running. In this way, based on statistics for each component for each UID/app, Power Tutor obtained the power consumption for each UID/app. Then summing them up to get the power consumption for the entire system.

If your app is running as super user program, the Android system assigns a UID of 0 to it. In this case, it breaks the Android rule of “each app is treated as a separate user”. Power Tutor shows UID 0 as “kernel” process.

Below are a screenshot of the stats for all running apps and a screenshot of the video converter app power consumption,

Figure 2. App-Based Power Measurement

How to Use it to Help Development

First of all, it can used to measure the power performance of your app. Because Power Tutor breaks down the power consumption to components, you can also get an idea what kind of activity is consuming the battery in your app. Is it network communication through WiFi? or too much CPU consumption?

Also Power Tutor measures the power based on statistics obtained from the /proc and /sys directories. It indirectly reflects the usage of hardware components. Is your app CPU bound, if your app is constantly consumes a lot of CPU component power? I give an example in another post here.

References:
1. Power Tutor website: http://powertutor.org/