Android 性能优化(7)网络优化( 3) Optimizing User-Initiated Network Use

 

Optimizing User-Initiated Network Use

This lesson teaches you to

  1. Pre-fetch Network Data
  2. Check for Connectivity or Listen for Changes
  3. Reduce the Number of Connections 

  Quick handling of user requests helps ensure a good user experience, especially when it comes to user actions that require network access. You should prioritize low latency over power conservation to provide the fastest response when optimizing network use that is a direct result of user actions. Attaining an optimal network traffic profile for your app, while making sure that your users get fast responses, can be a bit challenging.

  This lesson teaches you how to optimize network use for user-initiated actions and reduce battery consumption.

Pre-fetch Network Data

  https://youtu.be/Rk1u7VVmadE

  Pre-fetching data is an effective way to reduce the number of independent data transfer sessions that your app runs. With pre-fetching, when the user performs an action in your app, the app anticipates which data will most likely be needed for the next series of user actions and fetches that data in bulk. Battery power consumption is reduced for two reasons:

  • Because your app pre-fetches data only when the the mobile radio is already awake from the user's action, and so does not incur the overhead of waking up the mobile radio.
  • The app pre-fetches data for anticipated user actions, each of which might otherwise require separate requests that each incur waking up the mobile radio.

  Tip: To explore whether your app might benefit from pre-fetching, review your app's network traffic and look for situations where a specific series of user actions almost always results in multiple network requests over the course of the task. For instance, an app that incrementally downloads article content as a user views it might be able to pre-fetch one or more articles in categories the user is known to view.

  Watch the video on effective pre-fetching which describes what pre-fetching is, where to use it, and how much data to pre-fetch. For more details, see Optimizing Downloads for Efficient Network Access.

Check for Connectivity or Listen for Changes

  Searching for a cell signal is one of the most power-draining operations on a mobile device. Your app should always check for connectivity before sending a user-initiated network request. If you use a scheduling service,Schedulers do this automatically for you.

  • If only certain buttons in your activity depend on a network connection, use Connectivity Manager to check for a network connection before sending the network request, as instructed in Monitor for Changes in Connectivity. If there's no network, the app can save battery by not forcing the mobile radio to search.
  • If your entire activity's user interface is non-functional without network access, then use Manipulate Broadcast Receivers on Demand. This technique listens for connectivity changes when your activity is in the foreground, and prevents network requests from proceeding when no connectivity exists. That is, if your app detects that connectivity has been lost, it disables all of its receivers, except for the connectivity-change receiver. An example would be a news app that presents an activity with a full-screen view of news snippets and does no pre-fetching. Any snippet a user taps would require a network connection.

  A best practice for user-initiated traffic is to first check for a connection using Connectivity Manager, and if there is no connection, schedule the network request for when the connection is made. Schedulers will use techniques such as exponential backoff to save battery, where each time the attempt to connect fails, the scheduler doubles the delay before the next retry.

  Note: To check for connectivity for app-initiated traffic, see Optimizing App-Initiated Network Use.

Reduce the Number of Connections

  In general, it's more efficient to reuse existing network connections than to initiate new ones. Reusing connections also allows the network to more intelligently react to congestion and related network data issues. For more information on reducing the number of connections used by your app, see Optimizing Downloads for Efficient Network Access.

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值