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)
StrictMode (android. os. StrictMode) class can be used to enable and enforce various policies that can be checked for and reported upon. This can be a StrictMode violation in executing a disk write violation that occurs when you are performing disk writes on the main UI thread.
StrictMode is a developer tool that detects things you might be doing by accident and brings them to your attention so you can fix them. StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place.
To enable and configure the StrictMode in your application, you require to use setThreadPolicy() and setVmPolicy() methods of StrictMode. It is a good practice to set policies either in Application class or initial activity.
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.
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());
…
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With