Wednesday, May 25, 2016

Optimize power consumption in android app development

Optimize power consumption in android app

For mobile application development it is compulsory that application should not consume more power. In order to provide end users with good battery performance, device manufacturers have a joint responsibility together with all of app developers. Now a day’s mobile operating system provides whole and sole solution for user. User doesn’t need to use their laptops or PCs for small works like e-mail, documents reading etc.

I am sharing few programming things that should be taken care by developer to develop an application with minimum power consumption.

Reducing Network Battery Drain

The network traffic generated by an app can have a significant impact on the battery life of the device where it is running. In order to optimize that traffic, you need to both measure it and identify its source.

 In order to properly optimize your app's use of networking resources, you must understand how frequently your app is using the network and for what reasons.

Analyze how much network traffic use in android application and try to reduce the same for example Wi-Fi network consume less power instead of mobile data therefore if you have some downloading secondary task that can be allow to download in Wi-fi network only.

Use network only when you required do not make unnecessary call in application that request continuously in network with server.

Always optimize background services that should not be continuously running without necessary task.

Network Traffic Tool:

The Network Traffic tool in Android Studio helps you see how your app uses network resources in real time.

To start the Network Traffic tool and visualize the network requests:
1.       Start the Network Traffic tool by launching Android Studio and choosing Tools > Android > Android Device Monitor. When asked, allow incoming network connections.
2.       In the Android Device Monitor window, click the DDMS button along the top and choose the Network Statistics tab. If you don't see this tab, widen the window and then try Window > Reset Perspective.
3.       Select your app to debug from the list of debuggable apps on your device in the Devices tab, then click the Start button in the Network Statistics tab.
  

Optimizing for Doze and App Standby

Doze mode from android 6.0 and higher can affect you application for power reduction therefore at the time of development must test your app with Doze mode and optimize your app.

Doze and App Standby manage the behavior of all apps running on Android 6.0 or higher, regardless whether they are specifically targeting API level 23. To ensure the best experience for users, test your app in Doze and App Standby modes and make any necessary adjustments to your code.

Doze can affect apps differently. You must optimize the way that your app manages network, alarms, jobs, and syncs. Apps should be able to efficiently manage activities during each maintenance window.

Way to test android app with doze mode:

1.       Configure a hardware device or virtual device with an Android 6.0 or higher system image.
2.       Connect the device to your development machine and install your app.
3.       Run your app and leave it active.
4.       Shut off the device screen. (The app remains active.)
5.       Force the system to cycle through Doze modes by running the following commands:

$ adb shell dumpsys battery unplug
$ adb shell dumpsys deviceidle step

You may need to run the second command more than once. Repeat it until the device state changes to idle.

6.       Observe the behavior of your app after you reactivate the device. Make sure the app recovers gracefully when the device exits Doze.

Monitoring the Battery Level and Charging State

The battery-life impact of performing application updates depends on the battery level and charging state of the device. 

In some cases it's also useful to determine the current battery level. You may choose to reduce the rate of your background updates if the battery charge is below a certain level.

Optimize application code for background task or network access according to certain battery limit it make application more smooth and reduce the chance of data lose.

Monitoring the Docking State and Type

Android devices can be docked into car or home, digital and analog docks. Docking state means generally your charging state.

You may choose to increase the update frequency of a sports center app when it's in the desktop dock, or disable your updates completely if the device is car docked. Conversely, you may choose to maximize your updates while car docked if your background service is updating traffic conditions.

Determine docking state:

Register receiver:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
Intent dockStatus = context.registerReceiver(null, ifilter);

Extract the current docking status from the EXTRA_DOCK_STATE extra

int dockState = battery.getIntExtra(EXTRA_DOCK_STATE, -1);
boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;

Monitoring the Connectivity Status

 Most of the android application repeating alarms and background services is to schedule regular updates of application data from Internet resources. It might be possible that there is no network or network is very slow to download data in that case network should be monitored and should not run scheduler it is completely meaningless as well as consume power of device.












2 comments:

  1. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information...
    Mobile app development company Nashik /\ Android app development in Nashik

    ReplyDelete