Android 性能优化(8)网络优化( 4)Optimizing App-Initiated Network Use

Optimizing App-Initiated Network Use

  Network traffic initiated by your app can usually be significantly optimized, since you can plan for what network resources it needs and set a schedule for accessing them. By applying careful scheduling, you can create significant periods of rest for the device radio and, thereby, save power. There are several Android APIs that can help with network access scheduling, and some of these functions can coordinate network access for other apps, further optimizing battery performance.

  This lesson teaches you how to reduce battery consumption by applying techniques for optimizing app-initiated network traffic.

Batch and Schedule Network Requests

   https://youtu.be/Ecz5WDZoJok

  On a mobile device, the process of turning on the radio, making a connection, and keeping the radio awake uses a large amount of power. For this reason, processing individual requests at random times can consume significant power and reduce battery life. A more efficient approach is to queue a set of network requests and process them together. This allows the system to pay the power cost of turning on the radio just once, and still get all the data requested by an app.

   https://youtu.be/orlRuEwlDoM

  Using a network access scheduler API for queuing and processing your app data requests can significantly increase the power efficiency of your app. Schedulers conserve battery power by grouping requests together for the system to process. They can further improve efficiency by delaying some requests until other requests wake up the mobile radio, or waiting until the device is charging. Schedulers defer and batch network requests system-wide, across all apps on the device, which gives them an optimizing advantage over what any individual app can do.

Choosing a batch-and-scheduling API

  Android provides three different APIs for your app to batch and schedule network requests. For most operations, these techniques are functionally equivalent. These APIs are listed in the following table with the most highly recommended first.

SchedulerRequirementsImplementation Ease
GCM Network ManagerGCM Network Manager requires that your app use the Google Play services client library, version 6.1.11 or higher — use the latest available version.Straightforward
Job SchedulerJob Scheduler does not require Google Play services, but is available only when targeting Android 5.0 (API level 21) or higher.Straightforward
Sync Adapter for scheduled syncsSync Adapter does not require the Google Play services client library and has been available since Android 2.0 (API level 5).Complex

  Note: For scheduled data synchronization, you should always prefer GCM Network Manager or Job Scheduler over Sync Adapter if your requirements allow it.

Allow System to Check for Connectivity

  One of the most serious and unexpected causes of battery drain is when a user travels beyond the reach of any cell tower or access point. In this situation, the user is typically not using their device, but they notice the device getting warm, and then see that the battery is low or has run out.

  In this scenario, the problem is that an app is running a background process that keeps waking up the mobile radio at regular intervals to search for a cellular signal, but finds none. Searching for a cell signal is one of the most power-draining operations there is.

  The way to avoid causing this kind of problem for a user with your app is to use a battery-efficient method for checking connectivity. For app-initiated network requests, use a scheduler, which automatically usesConnectivity Manager to check for connectivity before calling into your app. As a result, if there's no network, the Connectivity Manager conserves battery because it performs the connectivity check itself, without loading the app to run the check. Battery is further conserved because schedulers use exponential backoff to check for connectivity less frequently as time progresses.

 

 

转载于:https://www.cnblogs.com/sjjg/p/5393208.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值