web-dev-qa-db-ja.com

キャッシュインAndroid webview

Android webview;キャッシュをロードするか、まったくロードしないかで、モバイルWebページと非モバイルWebページをロードするより速い方法はどれですか?

そして、それをロードするための推奨スタイルは何ですか?

現時点では、モバイル以外のサイトでキャッシュをロードしないと、ネイティブブラウザでキャッシュをロードするよりもはるかに遅くなります。

30
Eljas

これらを使用しないでください:

_viewer.getSettings().setAppCacheMaxSize(1024*1024*8);   
viewer.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache"‌​);    
viewer.getSettings().setAppCacheEnabled(true);   
_

これらは、デフォルトのwebview内部キャッシュとは関係ありません。 Appcacheは完全に異なる機能であり、インターネット接続なしでWebサイトを実行できるようにします。それはそれほどうまく機能せず、おそらくあなたはそれを使いたくないでしょう。

これを設定すると:viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT)で十分です。

38
nurieta

もちろん、キャッシュされたアプローチの方が高速です。それが、そもそもキャッシングが存在する正確な理由です。

ただし、webviewのキャッシングを特に無効にしない限り問題はありません。しない場合-デフォルトでキャッシュを使用します。

15
Ivan Bartsov
    /*
        public abstract void setAppCacheEnabled (boolean flag)
            Sets whether the Application Caches API should be enabled. The default is false.
            Note that in order for the Application Caches API to be enabled, a valid database
            path must also be supplied to setAppCachePath(String).

        Parameters
            flag : true if the WebView should enable Application Caches
    */
    // Enable the caching for web view
    mWebView.getSettings().setAppCacheEnabled(true);

    /*
        public abstract void setAppCachePath (String appCachePath)
        Sets the path to the Application Caches files. In order for the Application Caches
        API to be enabled, this method must be called with a path to which the application
        can write. This method should only be called once: repeated calls are ignored.

        Parameters
            appCachePath : a String path to the directory containing Application Caches files.
    */
    /*
        public abstract File getCacheDir ()
            Returns the absolute path to the application specific cache directory on the
            filesystem. These files will be ones that get deleted first when the device runs
            low on storage. There is no guarantee when these files will be deleted.

            Note: you should not rely on the system deleting these files for you; you should
            always have a reasonable maximum, such as 1 MB, for the amount of space you consume
            with cache files, and Prune those files when exceeding that space.

            The returned path may change over time if the calling app is moved to an adopted
            storage device, so only relative paths should be persisted.

            Apps require no extra permissions to read or write to the returned path,
            since this path lives in their private storage.

        Returns
            The path of the directory holding application cache files.
    */
    /*
        public String getPath ()
            Returns the path of this file.
    */
    // Specify the app cache path
    mWebView.getSettings().setAppCachePath(mContext.getCacheDir().getPath());

    /*
        public abstract void setCacheMode (int mode)
            Overrides the way the cache is used. The way the cache is used is based on the
            navigation type. For a normal page load, the cache is checked and content is
            re-validated as needed. When navigating back, content is not re-validated, instead
            the content is just retrieved from the cache. This method allows the client to
            override this behavior by specifying one of
                LOAD_DEFAULT,
                LOAD_CACHE_ELSE_NETWORK,
                LOAD_NO_CACHE or
                LOAD_CACHE_ONLY.
            The default value is LOAD_DEFAULT.

        Parameters
            mode : the mode to use
    */
    /*
        public static final int LOAD_DEFAULT
            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 setCacheMode(int).

        Constant Value: -1 (0xffffffff)
    */
    /*
        public static final int LOAD_CACHE_ELSE_NETWORK
            Use cached resources when they are available, even if they have expired. Otherwise
            load resources from the network. Use with setCacheMode(int).

        Constant Value: 1 (0x00000001)
    */
    /*
        public static final int LOAD_NO_CACHE
            Don't use the cache, load from the network. Use with setCacheMode(int).

        Constant Value: 2 (0x00000002)
    */
    /*
        public static final int LOAD_CACHE_ONLY
            Don't use the network, load from the cache. Use with setCacheMode(int).

        Constant Value: 3 (0x00000003)
    */
    // Set the cache mode
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
1
Masood