Android WebView 导致 StrictMode 违规

发布于 2024-12-11 23:19:34 字数 2618 浏览 0 评论 0原文

我注意到 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 技术交流群。

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

发布评论

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

评论(3

失眠症患者 2024-12-18 23:19:34

有没有办法提高WebView对象的启动性能?

与特定的 StrictMode 投诉无关,因为它来自 WebView 构造函数。

一般来说,您对 WebView 触发 StrictMode 无能为力——因为它是一个小部件,所以对您来说使用 做太多事情可能并不安全。 WebView 在后台线程上,特别是当它是视图层次结构的一部分时。

您可以扫描 http://b.android.com 以查看是否存在与此相关的现有错误报告,以及不,创建一个生成消息的示例项目并将其与错误报告一起归档。

Is there any way to improve the startup performance of a WebView object?

Not with respect to that specific StrictMode complaint, since it is coming from the WebView constructor.

In general, there is nothing you can do about WebView triggering StrictMode -- since it is a widget, it may not be safe for you to do much with the WebView 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.

兔姬 2024-12-18 23:19:34

对我有用的解决方案(Android 5.0)是在设置 StrictMode 之前创建一个虚拟 WebView:

…
new WebView(this);

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build());

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build());
…

A solution that worked for me (Android 5.0) was to create a dummy WebView before setting StrictMode:

…
new WebView(this);

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build());

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build());
…
清泪尽 2024-12-18 23:19:34

如果您对 StrictMode 违规感到困扰或者它使您的应用程序崩溃,您可以使用 StrictMode.allowThreadDisk*()

ThreadPolicy realPolicy = StrictMode.allowThreadDiskReads();
try {
    web = new WebView(container.getContext());
} finally {
    StrictMode.setThreadPolicy(realPolicy);
}

但是,遗憾的是允许读取还不够:

D/StrictMode: StrictMode policy violation; ~duration=546 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=245 violation=1
    at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1755)
    at android.webkit.WebViewDatabase.getInstance(WebViewDatabase.java:243)
    at android.webkit.WebView.<init>(WebView.java:981)
    at android.webkit.WebView.<init>(WebView.java:958)
    at android.webkit.WebView.<init>(WebView.java:948)
    at android.webkit.WebView.<init>(WebView.java:939)
    at MyFragment.onCreateView(CategoryHelpFragment.java:41)

所以最终的解决方法是:

ThreadPolicy realPolicy = StrictMode.allowThreadDiskWrites();
try { // in older versions of Android WebViewDatabase.getInstance creates/opens a DB and StrictMode complains
    web = new WebView(container.getContext());
} finally {
    StrictMode.setThreadPolicy(realPolicy);
}

我使用了手动 < code>WebView 创建,但您可以将其放在 setContentView 周围。

If you're bothered by the StrictMode violation or it crashes your app you can use StrictMode.allowThreadDisk*():

ThreadPolicy realPolicy = StrictMode.allowThreadDiskReads();
try {
    web = new WebView(container.getContext());
} finally {
    StrictMode.setThreadPolicy(realPolicy);
}

But, sadly allowing reads is not enough:

D/StrictMode: StrictMode policy violation; ~duration=546 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=245 violation=1
    at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1755)
    at android.webkit.WebViewDatabase.getInstance(WebViewDatabase.java:243)
    at android.webkit.WebView.<init>(WebView.java:981)
    at android.webkit.WebView.<init>(WebView.java:958)
    at android.webkit.WebView.<init>(WebView.java:948)
    at android.webkit.WebView.<init>(WebView.java:939)
    at MyFragment.onCreateView(CategoryHelpFragment.java:41)

So the final workaround is:

ThreadPolicy realPolicy = StrictMode.allowThreadDiskWrites();
try { // in older versions of Android WebViewDatabase.getInstance creates/opens a DB and StrictMode complains
    web = new WebView(container.getContext());
} finally {
    StrictMode.setThreadPolicy(realPolicy);
}

I used manual WebView creation, but you can just put this around setContentView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文