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:
- ConnectivityManager mConnectivity; TelephonyManager mTelephony ;......
- // Check the network connection. If no network is available, no network connection is required.
- NetworkInfo info = mConnectivity. getActiveNetworkInfo ();
- If (info = null |! MConnectivity. getBackgroundDataSetting ()){
- Return false;
- }
- // Determine the network connection type. Only data in 3G or wifi is updated.
- 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;
- }
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:
- Import java.util.zip. GZIPInputStream;
- HttpGet request = new HttpGet ();
- HttpResponse resp = new defaulthttpclient(cmd.exe cute (request );
- HttpEntity entity = response. getEntity ();
- InputStream compressed = entity. getContent ();
- 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:
- 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 );
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:
- Public void onCreate (){
- 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 ){
- Update unregisterReceiver (mReceiver );
- MHandler. removeMessages (MSG_BATT );
- MHandler. obtainMessage (MSG_BATT, intent). sendToTarget ();
- }
- };
Reduce android Application Power Consumption
Http://www.eoeandroid.com/thread-72478-1-1.html