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),
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.