평범한 개발자 행복한 가족, 패밀리그램

개발/안드로이드

Android WebView 캐시 모드

패밀리그램 2016. 12. 1. 23:51

안드로이드 WebView는 Cache Mode (캐시 모드)를 지원한다.

1
2
3
4
5
6
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
cs

위 와 같이 WebView의 Settings 객체를 가져와 캐시모드를 설정 할 수 있다.

(변하지 않는 FrontEnd 페이지 URL을 사용하기 때문에 LOAD_CACHE_ELSE_NETWORK 로 설정되어 있다.)


WebView에서 지원하는 CacheMode는 총 5가지 가 있다.

아래 코드는 CacheMode에 대한 Comment가 붙어있는 Android WebSettings의 코드를 가져 온 것이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
     * Default cache usage mode. If the navigation type doesn't impose any
     * specific behavior, use cached resources when they are available
     * and not expired, otherwise load resources from the network.
     * Use with {@link #setCacheMode}.
     */
    public static final int LOAD_DEFAULT = -1;
 
    /**
     * Normal cache usage mode. Use with {@link #setCacheMode}.
     *
     * @deprecated This value is obsolete, as from API level
     * {@link android.os.Build.VERSION_CODES#HONEYCOMB} and onwards it has the
     * same effect as {@link #LOAD_DEFAULT}.
     */
    @Deprecated
    public static final int LOAD_NORMAL = 0;
 
    /**
     * Use cached resources when they are available, even if they have expired.
     * Otherwise load resources from the network.
     * Use with {@link #setCacheMode}.
     */
    public static final int LOAD_CACHE_ELSE_NETWORK = 1;
 
    /**
     * Don't use the cache, load from the network.
     * Use with {@link #setCacheMode}.
     */
    public static final int LOAD_NO_CACHE = 2;
 
    /**
     * Don't use the network, load from the cache.
     * Use with {@link #setCacheMode}.
     */
    public static final int LOAD_CACHE_ONLY = 3;
cs

지원하는 캐시모드는 총 5가지가 있다.

static url을 로딩 하는 경우라면 LOAD_CACHE_ELSE_NETWORK을 사용하여 로딩속도를 대폭 향상 시킬 수 있다.

반응형