Android webview 中的缓存

发布于 2025-01-02 03:38:54 字数 126 浏览 5 评论 0原文

Android webview中加载移动网页和非移动网页哪一种更快;加载缓存还是根本不加载?

加载它的推荐样式是什么?

现在,当我不在所有非移动网站上加载缓存时,加载速度比在本机浏览器中加载它们时要慢得多。

Which one is faster way to load mobile web pages and non mobile web pages in Android webview; loading cache or not loading that at all?

And what is recommend style to load that?

Right now when I don't load cache at all non mobile sites are much more slower to load than when I load them in native browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海夜未眠 2025-01-09 03:38:54

不要使用这些:

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

这些与默认的 webview 内部缓存无关。 Appcache 是一个完全不同的功能,它使您能够在没有互联网连接的情况下运行网站。它的效果不太好,可能您不想使用它。

通过设置:viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT)就足够了。

Don't use these:

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

These have nothing to do with the default webview internal cache. Appcache is an entirely different feature mean to make you able to run the website w/o an internet connection. It does not work that great and probably you do not want to use it.

With setting this: viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT) is enough.

涫野音 2025-01-09 03:38:54

当然,缓存的方法应该更快。这就是缓存存在的确切原因。

但除非你专门禁用 webview 的缓存,否则应该没问题。如果不这样做 - 默认情况下它将使用缓存。

Of course, cached approach should be faster. That's the exact reason caching is there in the first place.

But you should be fine unless you specifically disable caching for webview. If you don't - it will use cache by default.

谁与争疯 2025-01-09 03:38:54
    /*
        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);
    /*
        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);
转角预定愛 2025-01-09 03:38:54
 mWebView.getSettings().setAppCacheEnabled(true);

这里 setAppCacheEnabled(true) 在 Target SDK 33 中已弃用

请改用 settings.cacheMode = WebSettings.LOAD_DEFAULT

 mWebView.getSettings().setAppCacheEnabled(true);

Here setAppCacheEnabled(true) is deprecated in Target SDK 33.

Use settings.cacheMode = WebSettings.LOAD_DEFAULT instead.

可是我不能没有你 2025-01-09 03:38:54

利用这些设置;他们将帮助您缓存 WebView 内容:

with(webSettings) {
        javaScriptEnabled = true
        domStorageEnabled = true
        databaseEnabled = true
        mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
        setThirdPartyCookiesEnabled(true)
        allowFileAccess = false
        allowContentAccess = false
    }

Utilize these settings; they will assist you in caching WebView content:

with(webSettings) {
        javaScriptEnabled = true
        domStorageEnabled = true
        databaseEnabled = true
        mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
        setThirdPartyCookiesEnabled(true)
        allowFileAccess = false
        allowContentAccess = false
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文