{"id":491,"date":"2022-12-06T13:35:52","date_gmt":"2022-12-06T05:35:52","guid":{"rendered":"https:\/\/iichen.cn\/?p=491"},"modified":"2022-12-06T13:35:52","modified_gmt":"2022-12-06T05:35:52","slug":"android-mutablecontextwrapper%e5%92%8cidlehandler","status":"publish","type":"post","link":"https:\/\/iichen.cn\/?p=491","title":{"rendered":"Android-MutableContextWrapper\u548cIdleHandler"},"content":{"rendered":"<ul>\n<li>MutableContextWrapper \u53ef\u4ee5\u521d\u59cb\u9884\u52a0\u8f7d\u4f7f\u7528\u5176\u4ed6Context\uff0c\u771f\u6b63\u4f7f\u7528\u4f20\u5165\u5bf9\u5e94\u7684Activity\u7684context<\/li>\n<\/ul>\n<pre><code class=\"language-java line-numbers\">WebSettings webSettings = myWebView.getSettings();\n\nwebSettings.setCacheMode(WebSettings.LOAD_DEFAULT);\n\nwebSettings.setDomStorageEnabled(true);\n\nwebSettings.setDatabaseEnabled(true);\nfinal String dbPath = getApplicationContext().getDir(\"db\", Context.MODE_PRIVATE).getPath();\nwebSettings.setDatabasePath(dbPath);\n\nwebSettings.setAppCacheEnabled(true);\nfinal String cachePath = getApplicationContext().getDir(\"cache\", Context.MODE_PRIVATE).getPath();\nwebSettings.setAppCachePath(cachePath);\nwebSettings.setAppCacheMaxSize(5*1024*1024);\n\nwebSettings.setJavaScriptEnabled(true);\n<\/code><\/pre>\n<pre><code class=\"language-java line-numbers\">public class PreloadWebView {\n    private PreloadWebView(){}\n\n    private static final int CACHED_WEBVIEW_MAX_NUM = 2;\n    private static final Stack&lt;WebView&gt; mCachedWebViewStack = new Stack&lt;&gt;();\n\n\n    public static PreloadWebView getInstance(){\n        return Holder.INSTANCE;\n    }\n\n    private static class Holder{\n        private static final PreloadWebView INSTANCE = new PreloadWebView();\n    }\n\n    \/**\n     * \u521b\u5efaWebView\u5b9e\u4f8b\n     * \u7528\u4e86applicationContext\n     *\/\n    public void preload() {\n        L.d(\"webview preload\");\n        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {\n            @Override\n            public boolean queueIdle() {\n                if (mCachedWebViewStack.size() &lt; CACHED_WEBVIEW_MAX_NUM) {\n                    mCachedWebViewStack.push(createWebView());\n                }\n                return false;\n            }\n        });\n    }\n\n    private WebView createWebView() {\n        WebView webview = new WebView(new MutableContextWrapper(App.getApp()));\n        webview.getSettings().setJavaScriptEnabled(true);\n        webview.loadDataWithBaseURL(\"file:\/\/\/android_asset\/article\/?item_id=0&amp;token=0\",getHtml(),\"text\/html\",\"utf-8\",\"file:\/\/\/android_asset\/article\/?item_id=0&amp;token=0\");\n        return webview;\n    }\n\n\n    private static String getHtml() {\n        StringBuilder builder = new StringBuilder();\n        builder.append(\"&lt;!DOCTYPE html&gt;\n\");\n        builder.append(\"&lt;html&gt;\n\");\n        builder.append(\"&lt;head&gt;\n\");\n        builder.append(\"&lt;meta charset=\"utf-8\"&gt;\n\");\n        builder.append(\"&lt;meta name=\"viewport\" content=\"initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"&gt;\n\");\n        builder.append(\"&lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"\");\n        builder.append(\"file:\/\/\/android_asset\/article\/css\/android.css\");\n        builder.append(\"\"&gt;\n&lt;\/head&gt;\n\");\n        builder.append(\"&lt;body class=\"font_m\"&gt;&lt;header&gt;&lt;\/header&gt;&lt;article&gt;&lt;\/article&gt;&lt;footer&gt;&lt;\/footer&gt;\");\n        builder.append(\"&lt;script type=\"text\/javascript\" src=\"\");\n        builder.append(\"file:\/\/\/android_asset\/article\/js\/lib.js\");\n        builder.append(\"\"&gt;&lt;\/script&gt;\");\n        builder.append(\"&lt;script type=\"text\/javascript\" src=\"\");\n        builder.append(\"file:\/\/\/android_asset\/article\/js\/android.js\");\n        builder.append(\"\" &gt;&lt;\/script&gt;\n\");\n        builder.append(\"&lt;\/body&gt;\n\");\n        builder.append(\"&lt;\/html&gt;\n\");\n        return builder.toString();\n    }\n\n    \/**\n     * \u4ece\u7f13\u5b58\u6c60\u4e2d\u83b7\u53d6\u5408\u9002\u7684WebView\n     *\n     * @param context activity context\n     * @return WebView\n     *\/\n    public WebView getWebView(Context context) {\n        \/\/ \u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de\u65b0\u5b9e\u4f8b\n        if (mCachedWebViewStack == null || mCachedWebViewStack.isEmpty()) {\n            WebView web = createWebView();\n            MutableContextWrapper contextWrapper = (MutableContextWrapper) web.getContext();\n            contextWrapper.setBaseContext(context);\n            return web;\n        }\n        WebView webView = mCachedWebViewStack.pop();\n        \/\/ webView\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5f00\u59cb\u4f7f\u7528\u9884\u521b\u5efa\u7684WebView,\u5e76\u4e14\u66ff\u6362Context\n        MutableContextWrapper contextWrapper = (MutableContextWrapper) webView.getContext();\n        contextWrapper.setBaseContext(context);\n        return webView;\n    }\n\n\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MutableContextWrapper \u53ef\u4ee5\u521d\u59cb\u9884\u52a0\u8f7d\u4f7f\u7528\u5176\u4ed6Context\uff0c\u771f\u6b63\u4f7f\u7528\u4f20\u5165\u5bf9\u5e94\u7684Activ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,8],"tags":[],"class_list":["post-491","post","type-post","status-publish","format-standard","hentry","category-android","category-8"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android-MutableContextWrapper\u548cIdleHandler - IIchen<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/iichen.cn\/?p=491\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android-MutableContextWrapper\u548cIdleHandler - IIchen\" \/>\n<meta property=\"og:description\" content=\"MutableContextWrapper \u53ef\u4ee5\u521d\u59cb\u9884\u52a0\u8f7d\u4f7f\u7528\u5176\u4ed6Context\uff0c\u771f\u6b63\u4f7f\u7528\u4f20\u5165\u5bf9\u5e94\u7684Activ [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iichen.cn\/?p=491\" \/>\n<meta property=\"og:site_name\" content=\"IIchen\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-06T05:35:52+00:00\" \/>\n<meta name=\"author\" content=\"iichen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"iichen\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/iichen.cn\/?p=491#article\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/?p=491\"},\"author\":{\"name\":\"iichen\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"headline\":\"Android-MutableContextWrapper\u548cIdleHandler\",\"datePublished\":\"2022-12-06T05:35:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/iichen.cn\/?p=491\"},\"wordCount\":6,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"articleSection\":[\"Android\",\"\u7b14\u8bb0\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/iichen.cn\/?p=491#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/iichen.cn\/?p=491\",\"url\":\"https:\/\/iichen.cn\/?p=491\",\"name\":\"Android-MutableContextWrapper\u548cIdleHandler - IIchen\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/#website\"},\"datePublished\":\"2022-12-06T05:35:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/iichen.cn\/?p=491#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iichen.cn\/?p=491\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/iichen.cn\/?p=491#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/iichen.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android-MutableContextWrapper\u548cIdleHandler\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/iichen.cn\/#website\",\"url\":\"https:\/\/iichen.cn\/\",\"name\":\"IIchen\",\"description\":\"Just do it!\",\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/iichen.cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\",\"name\":\"iichen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"contentUrl\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"width\":940,\"height\":940,\"caption\":\"iichen\"},\"logo\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.iichen.cn\"],\"url\":\"https:\/\/iichen.cn\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android-MutableContextWrapper\u548cIdleHandler - IIchen","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/iichen.cn\/?p=491","og_locale":"zh_CN","og_type":"article","og_title":"Android-MutableContextWrapper\u548cIdleHandler - IIchen","og_description":"MutableContextWrapper \u53ef\u4ee5\u521d\u59cb\u9884\u52a0\u8f7d\u4f7f\u7528\u5176\u4ed6Context\uff0c\u771f\u6b63\u4f7f\u7528\u4f20\u5165\u5bf9\u5e94\u7684Activ [&hellip;]","og_url":"https:\/\/iichen.cn\/?p=491","og_site_name":"IIchen","article_published_time":"2022-12-06T05:35:52+00:00","author":"iichen","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"iichen"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/iichen.cn\/?p=491#article","isPartOf":{"@id":"https:\/\/iichen.cn\/?p=491"},"author":{"name":"iichen","@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"headline":"Android-MutableContextWrapper\u548cIdleHandler","datePublished":"2022-12-06T05:35:52+00:00","mainEntityOfPage":{"@id":"https:\/\/iichen.cn\/?p=491"},"wordCount":6,"commentCount":0,"publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"articleSection":["Android","\u7b14\u8bb0"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/iichen.cn\/?p=491#respond"]}]},{"@type":"WebPage","@id":"https:\/\/iichen.cn\/?p=491","url":"https:\/\/iichen.cn\/?p=491","name":"Android-MutableContextWrapper\u548cIdleHandler - IIchen","isPartOf":{"@id":"https:\/\/iichen.cn\/#website"},"datePublished":"2022-12-06T05:35:52+00:00","breadcrumb":{"@id":"https:\/\/iichen.cn\/?p=491#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/iichen.cn\/?p=491"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/iichen.cn\/?p=491#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/iichen.cn\/"},{"@type":"ListItem","position":2,"name":"Android-MutableContextWrapper\u548cIdleHandler"}]},{"@type":"WebSite","@id":"https:\/\/iichen.cn\/#website","url":"https:\/\/iichen.cn\/","name":"IIchen","description":"Just do it!","publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/iichen.cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c","name":"iichen","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/","url":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","contentUrl":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","width":940,"height":940,"caption":"iichen"},"logo":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.iichen.cn"],"url":"https:\/\/iichen.cn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=491"}],"version-history":[{"count":0,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/491\/revisions"}],"wp:attachment":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}