App developers, do you want to enhance the user experience, and reduce the power consumption & improve the network efficiency of your app? We are happy to share our knowledge in the area, and one way to do this is by continuously contributing to initiatives like the guide called Smarter Apps for Smarter Phones. Published by the GSM Association (GSMA) earlier this year, this is the perfect guide for app developers interested in increasing the efficiency of their apps while enhancing the user experience.
In this tutorial, Håkan Jonsson from our Technology office has picked out some of the areas from the guide that he thinks are most important for increasing power efficiency and network usage in mobile devices. You will find useful and hands-on tips, as well as some brand new code examples that will help you improve your app. Read more after the jump!
Hi! I’m Håkan Jonsson, a research engineer at Sony Mobile’s Technology office. In my work, I have found that perhaps one of the most overlooked, but also most critical, areas of app development is to make your app power and network efficient. By doing so, you provide a clearly enhanced user experience, and while that might not directly translate in better reviews and increased sales, by not enhancing the user experience, you might get the opposite effect. According to the guidelines in Smarter Apps for Smarter Phones publication, there are five areas related to user experience improvements that are very important – scheduling, compression, asynchronous requests, caching, and push notifications. Let’s examine these areas in more detail in the sections below.
Remember to schedule background data sync
As part of scheduling tasks for your app, you can reduce the signalling load from background data. This is an important step to reduce power consumption and network traffic. To achieve this, you can synch your app’s network activity requests, such as HTTP requests, with network activities from other apps. By batching requests together, you reduce the network traffic and power consumption.
To begin with, you should use AlarmManager with setInexactRepeat and an interval constant provided in AlarmManager. For example, use INTERVAL_HALF_HOUR, which will make sure your intents are sent at least every half hour, without specifying when within that interval the intents are sent. For more info and guidance to how to implement this in your code, look in the Smarter Apps for Smarter Phones guide, where a short code sample on scheduling is provided.
Another important note is to make sure you use AlarmManager.RTC and not AlarmManager.RTC_WAKEUP. This will ensure that the application won’t be woken up unless some other app is also to wake up. On Google services-equipped smartphones and tablets, schedulers run regularly and will make sure your app wakes up now and then.
Use compression to reduce the size of HTTP requests
Another way to reduce the network traffic is to make sure that if your application is using HTTP, it should support compression. This will reduce the overall size of the HTTP entity bodies, which will potentially improve the user experience by making your app appear faster. For some data formats, such as XML and JSON, the average size reduction is as high as 80%. In Android™, there is support for qzip and DEFLATE compression of HTTP content. This means that it will take less time to download HTTP data to your app, which will then be faster and give the user a better experience.
To use these compressions, you need to add the “Accept-Encoding” header to the request your app sends. Then the received content needs to be handled according to its “Content-Encoding” header. In the Smarter Apps for Smarter Phones guide, you can actually download a code example that shows how you can implement compression using interceptors, with the Apache HTTPClient.
Improve the user experience by using asynchronous requests
When writing apps for smartphones, you should always consider the risk of having requests being sent over a mobile network to be delayed. Or that you might not even receive a response at all. To make sure the user experience is as smooth as possible in such an unstable network environment, you should aim to make your app ready to deliver some results to the user even if not all requested data has arrived. This can be ensured by using asynchronous requests.
To help you implement this, I have created some asynchronous request code examples for download, and that illustrate three different ways of downloading an image from Developer World. I have also included an example of bad practice. Just unzip the file at the link above to access the examples.
- The first example, the incorrect way of solving the task, blocks the UI during the download and result in a bad user experience. On Android 4.0 (Ice Cream Sandwich) it actually results in an error message. This is obviously not a good way to use asynchronous requests, but is included to show what happens when poorly implemented.
- The second example uses an AsyncTask, and the third example handles a download in a separate thread, and uses the Handler mechanism to display progress in the UI. Both of these methods are good and easy ways to implement asynchronous requests, and can be used with most common file formats. These methods are described in the Android developers’ article Painless Threading.
- The fourth example is using byte wise download, which can be used for cases where you have a file format that is not supported by the Android framework, or if the file format is unknown. For all other cases, byte wise download is not recommended since it is more complicated.
Make your app appear faster with caching
If you use caching, it makes any network applications appear faster by using the most effective means of data storage or transfer. The purpose is to reduce the number of round-trips to a server through an expiration mechanism and to reduce network bandwidth through a validation mechanism. These mechanisms make it possible for both client and server to control and know when an application can use cached content, and when they need to download new content.
The cache is essential for network applications, especially when running in mobile networks. In the Android web browser, internal APIs are used to support HTTP caching. Since these APIs aren’t available to other applications, you will have to implement the support for caching yourself. In the Smarter Apps for Smarter Phones guide, there is a detailed description, including code examples, of how to implement caching.
Push notifications lets you save energy and network traffic
Many apps would benefit a lot from using push notifications more often, as it saves both energy and network traffic since data is pushed to the device when available, so that the device doesn’t have to poll the server regularly. To implement push notifications, Google Cloud Messaging (GCM) is a really good method to use since it’s a service that provides a simple mechanism to handle all aspects of pushing data from a server to an application. It works for Android 2.2 and later.
Help us keep the guide updated
So, have I inspired you to make your app even smarter? Or have you already implemented any of the methods described in this article? The Smarter Apps for Smarter Phones guide is meant to be continuously updated, so any comments or feedback is welcome. I will bring your opinion to the work with the next revision, so drop us a comment below and let us know what you think!
For further information on this topic, you can also check out my previous tutorial about reducing power consumption.
- Dive into the GSMA guide to developing Smarter Apps for Smarter Phones.
- Download the code examples implementing asynchronous requests.
- Check out this video from GSMA introducing smarter apps.
- Read Håkan’s previous tutorial on Reducing power consumption of connected apps.
- Have a look at Android developers’ article about Painless Threading.
- Learn more about push notifications in Google’s API Guide and Demo Application.