Android WebView 导致 StrictMode 违规
我注意到 StrictMode 对 WebView 创建的抱怨相当大。有没有办法提高WebView对象的启动性能?我在 onCreate() 方法中从 XML 进行实例化。
webView = (WebView) findViewById(R.id.webview);
webView.clearCache(true);
webView.getSettings().setDefaultFontSize(20);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
WebSettings wb = webView.getSettings();
wb.setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.setWebChromeClient(new MyWebChromeClient(this));
这是许多 StrictMode 警告中的第一个:
D/StrictMode(22781): StrictMode policy violation; ~duration=1869 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
D/StrictMode(22781): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745)
D/StrictMode(22781): at android.database.sqlite.SQLiteStatement.simpleQueryForLong(SQLiteStatement.java:106)
D/StrictMode(22781): at android.database.sqlite.SQLiteDatabase.getVersion(SQLiteDatabase.java:928)
D/StrictMode(22781): at android.webkit.WebViewDatabase.getInstance(WebViewDatabase.java:196)
D/StrictMode(22781): at android.webkit.WebView.<init>(WebView.java:1002)
D/StrictMode(22781): at android.webkit.WebView.<init>(WebView.java:979)
D/StrictMode(22781): at android.webkit.WebView.<init>(WebView.java:969)
D/StrictMode(22781): at java.lang.reflect.Constructor.constructNative(Native Method)
D/StrictMode(22781): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
D/StrictMode(22781): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
D/StrictMode(22781): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
D/StrictMode(22781): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
D/StrictMode(22781): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
D/StrictMode(22781): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
D/StrictMode(22781): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
D/StrictMode(22781): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
D/StrictMode(22781): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
D/StrictMode(22781): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:214)
D/StrictMode(22781): at android.app.Activity.setContentView(Activity.java:1660)
D/StrictMode(22781): at com.myapplication.StartActivity.onCreate(StartActivity.java:886)
I'm noticing that StrictMode is complaining quite loudly about WebView creation. Is there any way to improve the startup performance of a WebView object? I'm instantiating from XML in my onCreate() method.
webView = (WebView) findViewById(R.id.webview);
webView.clearCache(true);
webView.getSettings().setDefaultFontSize(20);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
WebSettings wb = webView.getSettings();
wb.setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.setWebChromeClient(new MyWebChromeClient(this));
Here is the first of many StrictMode warnings:
D/StrictMode(22781): StrictMode policy violation; ~duration=1869 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
D/StrictMode(22781): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745)
D/StrictMode(22781): at android.database.sqlite.SQLiteStatement.simpleQueryForLong(SQLiteStatement.java:106)
D/StrictMode(22781): at android.database.sqlite.SQLiteDatabase.getVersion(SQLiteDatabase.java:928)
D/StrictMode(22781): at android.webkit.WebViewDatabase.getInstance(WebViewDatabase.java:196)
D/StrictMode(22781): at android.webkit.WebView.<init>(WebView.java:1002)
D/StrictMode(22781): at android.webkit.WebView.<init>(WebView.java:979)
D/StrictMode(22781): at android.webkit.WebView.<init>(WebView.java:969)
D/StrictMode(22781): at java.lang.reflect.Constructor.constructNative(Native Method)
D/StrictMode(22781): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
D/StrictMode(22781): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
D/StrictMode(22781): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
D/StrictMode(22781): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
D/StrictMode(22781): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
D/StrictMode(22781): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
D/StrictMode(22781): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
D/StrictMode(22781): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
D/StrictMode(22781): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
D/StrictMode(22781): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:214)
D/StrictMode(22781): at android.app.Activity.setContentView(Activity.java:1660)
D/StrictMode(22781): at com.myapplication.StartActivity.onCreate(StartActivity.java:886)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与特定的
StrictMode
投诉无关,因为它来自WebView
构造函数。一般来说,您对
WebView
触发StrictMode
无能为力——因为它是一个小部件,所以对您来说使用做太多事情可能并不安全。 WebView
在后台线程上,特别是当它是视图层次结构的一部分时。您可以扫描 http://b.android.com 以查看是否存在与此相关的现有错误报告,以及不,创建一个生成消息的示例项目并将其与错误报告一起归档。
Not with respect to that specific
StrictMode
complaint, since it is coming from theWebView
constructor.In general, there is nothing you can do about
WebView
triggeringStrictMode
-- since it is a widget, it may not be safe for you to do much with theWebView
on a background thread, particularly once it is part of the view hierarchy.You might scan http://b.android.com to see if there are existing bug reports about this, and if not, create a sample project that generates the message and file it along with a bug report.
对我有用的解决方案(Android 5.0)是在设置 StrictMode 之前创建一个虚拟 WebView:
A solution that worked for me (Android 5.0) was to create a dummy WebView before setting StrictMode:
如果您对 StrictMode 违规感到困扰或者它使您的应用程序崩溃,您可以使用
StrictMode.allowThreadDisk*()
:但是,遗憾的是允许读取还不够:
所以最终的解决方法是:
我使用了手动 < code>WebView 创建,但您可以将其放在
setContentView
周围。If you're bothered by the StrictMode violation or it crashes your app you can use
StrictMode.allowThreadDisk*()
:But, sadly allowing reads is not enough:
So the final workaround is:
I used manual
WebView
creation, but you can just put this aroundsetContentView
.