Android WebView导致StrictMode违规

时间:2011-10-26 03:45:27

标签: android performance webview android-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)

3 个答案:

答案 0 :(得分:6)

  

有没有办法改善WebView对象的启动性能?

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

一般情况下,您无法对WebView触发StrictMode做任何事情 - 由于它是一个小部件,因此您可能无法安全地使用WebView后台线程,特别是一旦它是视图层次结构的一部分。

您可以扫描http://b.android.com以查看是否存在关于此的错误报告,如果没有,则创建一个生成消息的示例项目,并将其与错误报告一起提交。

答案 1 :(得分:3)

对我有用的解决方案(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());
…

答案 2 :(得分:0)

如果您对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);
}

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