跳转到相应内容

最常访问

最近访问

navigation

Redundant Downloads are Redundant

Many of your app's users have intermittent access to the Internet or have limits on the amount of information they can download onto their devices. You can encourage users to interact with your app more often by reducing the amount of data that your app needs to download.

The most fundamental way to reduce your downloads is to download only what you need. In terms of data, that means implementing REST APIs that allow you to specify query criteria that limit the returned data by using parameters such as the time of your last update.

Similarly, when downloading images, it's good practice to reduce the size of the images server-side, rather than downloading full-sized images that are reduced on the client.

Cache Files Locally

Another important technique is to avoid downloading duplicate data. You can reduce the likelihood of downloading the same piece of data repeatedly by using caching. By caching your app's data and resources, you create a local copy of the information that your app needs to reference. If your app needs to access the same piece of information multiple times over a short time period, you only need to download it into the cache once.

It's important to cache as aggressively as possible in order to reduce the total amount of data that you download. Always cache static resources, including on-demand downloads such as full size images, for as long as reasonably possible. On-demand resources should be stored separately to enable you to regularly flush your on-demand cache to manage its size.

To ensure that your caching doesn't result in your app displaying stale data, be sure to extract the time at which the requested content was last updated, and when it expires, from within the HTTP response headers. This will allow you to determine when the associated content should be refreshed.

// url represents the website containing the content to place into the cache.
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

long currentTime = System.currentTimeMillis();
long expires = conn.getHeaderFieldDate("Expires", currentTime);
long lastModified = conn.getHeaderFieldDate("Last-Modified", currentTime);

// lastUpdateTime represents when the cache was last updated.
if (lastModified < lastUpdateTime) {
    // Skip update
} else {
    // Parse update
    lastUpdateTime = lastModified;
}

Using this approach, you can also effectively cache dynamic content while ensuring it doesn't result in your application displaying stale information.

You can cache non-sensitive data in the unmanaged external cache directory:

Context.getExternalCacheDir();

Alternatively, you can use the managed, secure application cache. Note that this internal cache may be flushed when the system is running low on available storage.

Context.getCacheDir();

Note: Files stored in either cache location are erased when the app is uninstalled.

Use the HttpsURLConnection Response Cache

Android 4.0 added a response cache to HttpsURLConnection. You can enable HTTP response caching on supported devices using reflection as follows:

private void enableHttpResponseCache() {
  try {
    long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
    File httpCacheDir = new File(getCacheDir(), "http");
    Class.forName("android.net.http.HttpResponseCache")
         .getMethod("install", File.class, long.class)
         .invoke(null, httpCacheDir, httpCacheSize);
  } catch (Exception httpResponseCacheNotAvailable) {
    Log.d(TAG, "HTTP response cache is unavailable.");
  }
}

This sample code will turn on the response cache on Android 4.0+ devices without affecting earlier releases.

With the cache installed, fully cached HTTP requests can be served directly from local storage, eliminating the need to open a network connection. Conditionally cached responses can validate their freshness from the server, eliminating the bandwidth cost associated with the download.

Uncached responses get stored in the response cache for future requests.

此网站会使用 Cookie 来存储您在此网站上指定的语言和显示选项偏好设置。

获取最新的 Android Developers 资讯和提示,助您在 Google Play 上取得成功。

* 必填字段

成功!

在微信上关注 Google Developers

要以浏览此网站吗?

您请求访问的是网页,但是您为此网站设置的语言偏好为

要更改您的语言偏好设置并以浏览此网站吗?如果以后您想要更改语言偏好设置,请使用每个页面底部的语言菜单。

该类需要 或更高的 API 级别

此文档已被隐藏,因为您为该文档选择的 API 级别是 。您可以使用左侧导航栏上方的选择器来更改文档的 API 级别。

要详细了解如何根据您的应用需求指定 API 级别,请参阅支持不同平台版本

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)