[Android sharing] reducing the power consumption of android apps

Source: Internet
Author: User

First, let's take a look at the main usage of android phones:
Obviously, most of the electricity is consumed by network connections, GPS, and sensors. In short, it consumes a lot of power in the following situations:1. Transmission of large data volumes.2. Constantly switch between networks.3. parse a large amount of text data.

So how can we improve our program?

1. In the program that requires network connection, first check whether the network connection is normal. If there is no network connection, you do not need to execute the corresponding program. To check the network connection, follow these steps:

Java code:

  1. ConnectivityManager mConnectivity; TelephonyManager mTelephony ;......
  2. // Check the network connection. If no network is available, no network connection is required.
  3. NetworkInfo info = mConnectivity. getActiveNetworkInfo ();
  4. If (info = null |! MConnectivity. getBackgroundDataSetting ()){
  5. Return false;
  6. }
  7. // Determine the network connection type. Only data in 3G or wifi is updated.
  8. Int netType = info. getType ();
  9. Int netSubtype = info. getSubtype ();
  10. If (netType = ConnectivityManager. TYPE_WIFI ){
  11. Return info. isConnected ();
  12. } Else if (netType = ConnectivityManager. TYPE_MOBILE & netSubtype = TelephonyManager. NETWORK_TYPE_UMTS &&! MTelephony. isNetworkRoaming ()){
  13. Return info. isConnected ();
  14. } Else {
  15. Return false;
  16. }

Copy code

2. Use highly efficient data formats and resolution methods. Tests show that the current mainstream data formats are parsed in a tree (such as DOM) and a stream (SAX)

3. Currently, most websites support GZIP compression. Therefore, when downloading large amounts of data, try to use GZIP. The usage is as follows:

Java code:

  1. Import java.util.zip. GZIPInputStream;
  2. HttpGet request = new HttpGet ();
  3. HttpResponse resp = new defaulthttpclient(cmd.exe cute (request );
  4. HttpEntity entity = response. getEntity ();
  5. InputStream compressed = entity. getContent ();
  6. InputStream rawData = new GZIPInputStream (compressed );

Copy code

Downloading data using GZIP compression can reduce network traffic. You can use GZIP to obtain the RSS comparison of 1800 topics.

4. Other Optimization Methods: java objects are recycled, especially large java objects.XmlPullParserFactory and BitmapFactoryMatcher. reset (newString) for regexStringBuilder. sentLength (0)If the positioning requirement is not too high, try not to use GPS positioning. wifi and mobile network cell may be used for positioning. GPS Positioning consumes much more power than mobile network positioning. Do not use floating point operations. The cache technology can be used to obtain screen size and other information without multiple requests. Many people develop a program that uses a service to constantly update data on the server and sleep the service without updating data. This method consumes a lot of power. Generally, we can use AlarmManager to regularly start the service. As shown in the following figure, the task is executed once every 30th minutes.

Java code:

  1. AlarmManager am = (AlarmManager) context. getSystemService (Context. ALARM_SERVICE );
  2. Intent intent = new Intent (context, MyService. class );
  3. PendingIntent pendingIntent = PendingIntent. getService (context, 0, intent, 0 );
  4. Long interval = DateUtils. MINUTE_IN_MILLIS * 30;
  5. Long firstWake = System. currentTimeMillis () + interval; am. setRepeating (AlarmManager. RTC, firstWake, interval, pendingIntent );

Copy code

The last trick is to check the power before running your program. If the power consumption is too low, you will be prompted to charge your application. Haha! Usage:

Java code:

  1. Public void onCreate (){
  2. RegisterReceiver (mReceiver, mFilter );
  3. MHandler. sendEmptyMessageDelayed (MSG_BATT, 1000 );
  4. }
  5. IntentFilter mFilter = new IntentFilter (Intent. ACTION_BATTERY_CHANGED );
  6. BroadcastReceiver mReceiver = new BroadcastReceiver (){
  7. Public void onReceive (Context context, Intent intent ){
  8. Update unregisterReceiver (mReceiver );
  9. MHandler. removeMessages (MSG_BATT );
  10. MHandler. obtainMessage (MSG_BATT, intent). sendToTarget ();
  11. }
  12. };

 

Reduce android Application Power Consumption
Http://www.eoeandroid.com/thread-72478-1-1.html

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.