Realization of Electricity Optimization Based on Android Optimization

  • 2021-09-16 08:01:36
  • OfStack

After Android 5.0, the electric quantity was analyzed by Battery Historian tool.

Power consumption factor

Mobile network request

The mobile phone is connected with the base station through the built-in RF module, so as to link to the Internet. This RF module (radio) is very power-consuming. In order to control the power consumption of this RF module, the hardware driver and Android RIL layer have done a lot of processing. For example, radio (flight mode) can be turned off separately, radio can be turned off intermittently (power on when data occurs, and interact with base station at one frequency), etc. Today's App is a mobile Internet App, and it is inevitable that there will be a large number of network requests, which will cause radio 1 to be in an active state directly, thus increasing power consumption.

Using mobile network to transmit data, the power consumption has the following three states:

Full power: In the high power state, the mobile network connection is activated, allowing the device to operate at the maximum transmission rate. Low power: Low power consumption state, which consumes almost 50% of the power in Full power state. Standby: Idle state, no data connection needs to be transmitted, and the power consumption is the least.

From low power to high power about 1.5 s, from idle state to high power about 2s, seconds. Every time a new network connection is created in the application, the network (RF) module will switch to the high power state (Radio Full Power), and then switch back to the low power state (Radio Low Power) after data transmission. The transition process takes 5 seconds, and the power consumption of these 5 seconds is kept in the high power state, and finally it takes 12 seconds to switch to the idle state. Therefore, for a typical mobile network device, each data transmission will cause the network module to consume 20 seconds of power.

WakeLock

In order to optimize the use of power, Android system itself will go to sleep when it is not in operation to save power. Of course, Android provides an PowerManager. WakeLock for ease of development (many applications inevitably want to run something after the screen is turned off, or keep the screen straight on-such as playing video).

We can use WakeLock to keep CPU running, or prevent the screen from dimming/turning off, so that the phone can still do something when the user is not operating. However, it is easy to obtain WakeLock, and if it is not released well, it will become a problem and consume electricity. For example, get an WakeLock to keep CPU running, do a complex operation and upload the data to the background server, and then release the WakeLock. However, this process may not be as fast as we thought, because, for example, the server hangs up, an exception is calculated, etc., WakeLock is not released, and CPU will not sleep for 1 straight, which greatly increases power consumption.

In addition, WakeLock also has android: keepScreenOn attribute, which can also make the screen constant, which is also a big power consumer.


private void acquireWakeLock(Context ctx) { 
  if (null == mWakeLock) { 
    PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); 
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "TestLocknService"); 
    if (null != mWakeLock) { 
      mWakeLock. acquire(); 
    } 
  } 
} 
PARTIAL_WAKE_LOCK: Keep CPU running normally, and the screen and keyboard lights may be turned off. SCREEN_DIM_WAKE_LOCK: Leave CPU running, allowing the screen to remain displayed but possibly dimmed, allowing the keyboard lights to be turned off. SCREEN_BRIGHT_WAKE_LOCK: Keep CPU running, allowing the screen to be highlighted, allowing keyboard lights to be turned off. FULL_WAKE_LOCK: Keep CPU running, keep the screen highlighted, and keep the keyboard lights bright. ACQUIRE_CAUSES_ WAKEUP: Force the screen to light up. This lock is mainly used for actions that must notify the user. ON_AFTER_RELEASE: When the lock is released, keep the screen on for 1 period of time.

Registration permission is required


<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DEVICE_POWER"/>

GPS

Location services are often used in applications, and Android provides Network location and GPS location. Relatively speaking, GPS will be much more accurate, and for some applications such as running and navigation, GPS will basically be used for positioning. However, GPS positioning also consumes a lot of power.

AlarmManager

The interval should not be too short.

Optimization suggestion

Optimize network requests

In cellular mobile networks, it is best to execute network requests in batches, avoid frequent interval network requests as much as possible, and keep Radio Standby state as much as possible.

Try to use data transmission in Wi-Fi environment.

Use WakeLock with caution

WakeLock get releases occur in pairs (call release), using a timeout WakeLock in case an exception causes no release.

WakeLock has an interface setReferenceCounted, which is used to set the counting mechanism of WakeLock, true is counting, false is not counting, and the default is true. The so-called counting means that every acquire must correspond to one release; No counting means that no matter how many acquire there are, one release can be released. Although the official said that the default is counting, some third-party ROM has been modified so that the default is not counting.

Active setting wakeLock.setReferenceCounted(false)。

Monitor the charging status of mobile phone

BatteryManager will send a continuous broadcast containing the charging status, through which we can obtain the charging status and power details. Because this is a continuous broadcast, there is no need to write Receiver, and relevant data can be obtained directly through intent.


IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null , ifilter);
//  The device is charging 
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS , -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
         status == BatteryManager.BATTERY_STATUS_FULL;
//  You can also monitor the change of charging status, as long as the device is connected or disconnected from the power supply, BatteryManager  The corresponding operation will be broadcast 
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED , -1);
boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;

Another page can register Receiver to listen


<receiver android:name=".PowerConnectionReceiver">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
  </intent-filter>
</receiver>

Doze and App Standby

Android 6.0 provides two power saving technologies Doze and App Standby.

Doze sleepy. If the device is idle for a long time, Doze technology will reduce power consumption by delaying background network activities and CPU operation. App Standy application standby. Instead of the App that has recently been used by users, App Standy will delay the back-end network activity of this application.

Doze and App Standby runs on all Android 6.0 and above devices. It may affect the operation of App and can be adapted according to official documents.

You can adjust the design page of power optimization in the code, and let the user choose whether to add the application to the white list, so that you can do something in Doze mode.

Positioning

Use GPS in positioning and close it in time


// Remove the listener you previously added
locationManager.removeUpdates(locationListener);

Computational optimization

Shorten the running time of code generation instructions, thus reducing the total time taken by an application to CPU time slice, and thus reducing the percentage of power consumption of the whole system by the application per unit time.

Floating-point operations consume more CPU time slices than integer operations, so power consumption will also increase. Floating-point operations should be minimized in the process of coding.

Division changes multiplication. Make full use of displacement. Look-up table method, using mapping relationship directly, but this will increase memory consumption, depending on the situation.

Stop 1 operation related to UI effect after extinguishing the screen, such as animation.


Related articles: