By June this year, China's mobile phone users have reached 388 million, more than the computer terminal. I believe that the students who have smartphones have used their mobile phones to surf the internet. But the battery of the cell phone is running out quickly. I'm afraid it's one thing everyone can't stand. and opening the data connection for network data transmission is very expensive, if the user found that your application has drained his battery of electricity, I am afraid he will be the first time to uninstall your application. Therefore, it is necessary to develop the network application to understand how to save electricity.
If the data connection has been kept active, then its power consumption is very alarming, so the phone will be idle in the case of the network automatically hibernate data connection to achieve the purpose of saving electricity. As an example of a 3G network, it has three different states: full power: All work, network signal and power consumption are very high. Low power: Electricity is half the power, and of course the signal is worse. Standby: Low power consumption, equivalent to sleep. The phone will switch state based on the current usage of the network, as shown in this is the state machine of the wireless network. When the full power state is idle for 5 seconds, it enters the low power state and enters the standby state if it is idle for 12 seconds. Re-switching from the standby state to full power requires a 2-second delay, which takes 1.5 seconds from the low power state. Understanding this state machine is a good way to understand how to save power when transmitting network data. For example, we now have 3 seconds of data to transmit. One strategy is to transfer 1 seconds every 18 seconds, and the second is to pass the 3-second data all at once. Let's analyze how these two strategies affect state machines and their power consumption. The first: the transmission 1 seconds plus the waiting 5 seconds, in the full power state of 6 seconds. Transmitted 3 times, for a total of 18 seconds. In addition, each time in the low power state waiting for 12 seconds plus 1.5 seconds of delay, transmission 3 times is (12+1.5) x3=40.5 seconds. Second: The transmission 3 seconds plus the waiting 5 seconds, in the full power state of 8 seconds, and then wait 12 seconds after the low power state to enter the standby state. after analysis, the power consumption is drawn as a pie chart as shown. We can clearly see that the second strategy is much more power-saving than the first strategy. This is in fact the idea of packaging data transmission, which means that a lot of fragmented and compact data transfer into a single package to transfer can save a lot of power. In addition, we want to reduce the number of network connections as much as possible. Because each new network connection is more expensive than the amount of resources and power you have to take advantage of the existing network connection. So if there is a network connection that can be reused now, we have to prioritize these connections instead of creating a new connection. And the packaging idea in the first place is that if there are a lot of connection requests, we can also put these requests into a packet in a request, so we need a network connection. another way to reduce the number of network connections is to not wait for it to time out when the connection is not available, but to shut it down voluntarily. However, this may be in conflict with the aforementioned network connectivity reuse. The solution is not to close the connection immediately after use, wait for a period of time (of course, before the timeout) if it has not been used, then close the connection. Prefetch data is also an effective strategy for power saving. Take a look at the application of the news as an example, if there are 10 news, when the user looks at the first article we can take all the 10 news at once, is a similar packaging strategy. This strategy has the following advantages: reduces the number of times the network is activated, and reduces the amount of power wasted waiting while the state machine switches. improve the user experience, users can open the following news when they do not need to wait for the process of data transmission, it will open immediately, the response speed can be very fast. However, if the strategy is too much to use, it can be risky, but it may increase the power consumption of the program and consume the user's traffic. As to exactly how much to use to see the actual situation, generally speaking can be used to "round" the way that the user if there is a 50% probability that the data can be used to prefetch it. I believe everyone has seen online video, when you see 10 minutes, the video may have been down to 20 minutes, and then no longer downloaded, until you see such as 18 minutes, the video then download 10 minutes of content to the cache. This can actually be seen as an application of prefetch data. in the SDK Ddms for Android 4.0.4, there is a tool: Network traffic tool. This tool enables real-time monitoring of network usage, enabling programmers to better discover when their applications are sending the network data they receive, thereby improving their applications to save power. If you want to see more clearly how each network connection is used, you can tag the network connection in your program, such as a socket connection:
Click (here) to collapse or open
Trafficstats.setthreadstatstag (0xf00d);
Trafficstats.tagsocket (Outputsocket);
Transfer Data using socket
Trafficstats.untagsocket (Outputsocket);
for Apache HttpClient and URLConnection will automatically tag, so as long as you set the tag name on it:
Click (here) to collapse or open
Trafficstats.setthreadstatstag (0xf00d);
try {
Make network request using Httpclient.execute ()
} finally {
Trafficstats.clearthreadstatstag ();
}
that is, I am in a httpclient tag after the network usage, the system is marked with red to distinguish between:
Another area that can be very power-hungry is the application update. There are two main types of updates to be applied, namely:
Polling: Periodically connect to the server to request data. This is for the use of 3G network mobile phones, the program every request will be the mobile phone network state machine in the high power consumption of 20 seconds or so, the cumulative amount of wasted electricity is also very objective. It is not recommended to use this method. Google Cloud Messaging: A lightweight implementation of Google that allows the server to push the updated content to the target phone. It is recommended to use this method as compared to polling, which can save more electricity. The phone will always be in full power when the data is downloaded, and a long download will quickly run out of battery power. So if you think about saving electricity in this way, you need to reduce the amount of downloads. This is not contradictory to the prefetch data mentioned earlier, and what we have to reduce is to reduce unnecessary downloads. If you download a large picture, you can do the image processing on the server side, reduce the size of the picture, and then we download. It is also possible to cache data that is often used locally, reduce this duplication of downloads, and improve the user experience.
Finally, it is important to change the way you connect to your network based on real-time network connectivity. For example, now that WiFi is turned on, our application should not continue to use the 3G network, but to switch to WiFi. Because Wi-Fi can download large amounts of data at a higher bandwidth with a smaller power consumption. Remove WiFi, for data connection, the faster, the higher the power consumption, there are the following rules: LET>3G>2G. So in the same time, the high-speed network waits for the state machine to transform the process consumes more electricity. So in a better network situation can be considered more "over" the use of prefetch data policy. For more detailed information, please see Google's official documentation: http://developer.android.com/training/efficient-downloads/index.html
[Android traffic] makes Android apps more power-saving when transmitting network data