Controlling the power consumption of an android application

Source: Internet
Author: User
Tags comparison json

Take a look at what the Android phone's electricity consumption is mostly:

It is obvious that most of the electricity is consumed by network connections, GPS, sensors.

Simply put, the power consumption is mostly in the following situations:

1, large amount of data transmission.

2, Non-stop switching between the network.

3, the resolution of a large number of text data.

So how are we going to improve our program?

1, in need of network connection program, first check the network connection is normal, if there is no network connection, then do not need to implement the appropriate procedures.

The way to check network connectivity is as follows:

Connectivitymanager mconnectivity; Telephonymanager Mtelephony; ... check the network connection, if no network is available, do not need to do networkinfo info = Mconnectivity.getactivenetworkinfo (); if (info = null | |!mconnectivity.getbackgrounddatasetting ()) {return false;}//Determine the type of network connection, only some data updates are made in 3G or WiFi. int nettype = Info.gettype (); int netsubtype = Info.getsubtype (); if (NetType = = Connectivitymanager.type_wifi) {return info.isconnected ();} else if (NetType = = Connectivitymanager.type_ MOBILE && Netsubtype = = Telephonymanager.network_type_umts &&!mtelephony.isnetworkroaming ()) {return Info.isconnected (); else {return false;}

2, the use of high efficiency data format and analytical methods.

The test found that the current mainstream data format, using tree parsing (such as DOM) and the way the flow of parsing (SAX) comparison is shown in the following figure:

Obviously, the way to use a stream is more efficient, because DOM parsing is done after reading the entire document and then organizing it according to the node hierarchy. And the flow of the way is the edge of the reading data analysis, after the data read, parsing is finished.

In terms of data format, JSON and PROTOBUF are significantly more efficient than XML, and XML and JSON are well known to all, Protobuf is a language-independent, platform-independent, extensible, structured data serialization method for communication protocols and data storage. Interested can go to the official to see more information.

From the above diagram we can draw the conclusion that we try to parse the data using sax equal reading Edge parsing, for mobile devices, it is best to use the lightweight data format such as JSON.

3, the current large departments of the site to support gzip compression, so in the large amount of data download, as far as possible using the gzip way to download.

The use method looks like this:

Import Java.util.zip.GZIPInputStream; HttpGet request = new HttpGet ("Http://example.com/gzipcontent"); HttpResponse resp = new Defaulthttpclient (). Execute (Request); httpentity entity = response.getentity (); InputStream compressed = entity.getcontent (); InputStream rawdata = new Gzipinputstream (compressed);

Using gzip compression to download data to reduce network traffic, the following figure provides an RSS comparison of 1800 topics using the Gzip method.

4, some other optimization methods:

Reclaim Java objects, especially the larger Java-like

Xmlpullparserfactory and Bitmapfactory Matcher.reset (newstring) for regex stringbuilder.sentlength (0)

If the positioning requirements are not too high, try not to use GPS positioning, you may use WiFi and mobile network cell positioning can be. GPS positioning consumes far more power than mobile network positioning.

Try not to use floating-point operations.

To get information such as screen size, you can use caching techniques without requiring multiple requests.

Many people developed a program backstage will be a service non-stop to update the data on the server, in the not to update the data when let it sleep, this way is very power consumption, usually, we can use Alarmmanager to start the service regularly. As shown below, the first 30 minutes are executed.

Alarmmanager am = (alarmmanager) context.getsystemservice (Context.alarm_service); Intent Intent = new Intent (context, myservice.class); Pendingintent pendingintent = pendingintent.getservice (context, 0, intent, 0); Long interval = Dateutils.minute_in_millis * 30; Long Firstwake = System.currenttimemillis () + interval; Am.setrepeating (Alarmmanager.rtc,firstwake, interval, pendingintent);

The last trick, before running your program to check the electricity, the electricity is too low, then prompts the user to charge and so on, use the method:

public void OnCreate () {

Register for sticky broadcast and send default

Registerreceiver (Mreceiver, Mfilter);

Mhandler.sendemptymessagedelayed (Msg_batt, 1000);

}

Intentfilter Mfilter =

New Intentfilter (intent.action_battery_changed);

Broadcastreceiver mreceiver = new Broadcastreceiver () {

public void OnReceive (context context, Intent Intent) {

Found sticky broadcast, so trigger update

Unregisterreceiver (Mreceiver);

Mhandler.removemessages (Msg_batt);

Mhandler.obtainmessage (Msg_batt, intent). Sendtotarget ();

}

};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.