Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview not loading at first time

I'm struggle with a strange issue. I'm trying to load a https web page, but the first time the webview doesn't load. After wait 60 seconds, I have to click again in my button to load my page. My device is a Nexus 4 with Lollipop, but this issue happens at devices with Android 4.4 and 4.1 as well. The URL doesn't have heavy content only a few javascript files and css files.

Log:

 I/WebViewFactory﹕ Loading com.google.android.webview version 37 (1602158-arm) (code 111201)
 I/LibraryLoader﹕ Loading: webviewchromium
 I/LibraryLoader﹕ Time to load native libraries: 3 ms (timestamps 5331-5334)
 I/LibraryLoader﹕ Expected native library version number "",actual native library version number ""
 I/LibraryLoader﹕ Expected native library version number "",actual native library version number ""
 I/chromium﹕ [INFO:library_loader_hooks.cc(106)] Chromium logging enabled: level = 0, default verbosity = 0
 I/BrowserStartupController﹕ Initializing chromium process, renderers=0
 W/art﹕ Attempt to remove local handle scope entry from IRT, ignoring
 W/chromium﹕ [WARNING:resource_bundle.cc(315)] locale_file_path.empty()
 I/chromium﹕ [INFO:aw_browser_main_parts.cc(63)] Load from apk succesful, fd=72 off=159196 len=3264
 I/chromium﹕ [INFO:aw_browser_main_parts.cc(78)] Loading webviewchromium.pak from, fd:73 off:229484 len:643667
 W/AudioManagerAndroid﹕ Requires BLUETOOTH permission
 W/chromium﹕ [WARNING:proxy_service.cc(901)] PAC support disabled because there is no system implementation
 W/chromium﹕ [WARNING:data_reduction_proxy_settings.cc(403)] SPDY proxy OFF at startup
 W/art﹕ Attempt to remove local handle scope entry from IRT, ignoring
 W/AwContents﹕ onDetachedFromWindow called when already detached. Ignoring
 I/chromium﹕ [INFO:SkUtilsArm.cpp(179)] Device supports ARM NEON instructions!

My code:

final WebView wv = (WebView) alert.findViewById(R.id.modal_wv);

    wv.getSettings().setAppCacheEnabled(true);
    wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    wv.getSettings().setAppCachePath("/data/data/" + getPackageName() + "/cache");
    wv.getSettings().setAllowFileAccess(true);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl(connectionResponse.getUrl());
/*
//Same behavior ...
    wv.post(new Runnable() {
        @Override
        public void run() {
            wv.loadUrl(connectionResponse.getUrl());
        }
    });
*/

I set a new WebClient() overriding the following methods: shouldOverrideUrlLoading, onLoadResource, onPageFinished.For tests purpose, I removed this custom WebClient but it still wasn't load at the first time.

Thanks

like image 325
Esdras Avatar asked Jan 27 '15 03:01

Esdras


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

How do I open WebView?

Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.

How do I enable cookies on WebView?

How do I enable cookies in a webview? CookieManager. getInstance(). setAcceptCookie(true);


2 Answers

I met the same issue. I spent more than 3 days and found the cause it that another WebView object calls pauseTimers() for saving some CPU performance which actually " Pauses all layout, parsing, and JavaScript timers for all WebViews."

like image 71
Dangui Avatar answered Oct 27 '22 17:10

Dangui


Since you are loading an https url in webview, it will cause SSL certification issues so you have to override one more method for handling certification issue and handle it anyway here is the code how can you resolve it

myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            Toast.makeText(MainActivity.this, description,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onReceivedSslError(WebView view,
                SslErrorHandler handler, SslError error) {
            // TODO Auto-generated method stub
            // this method will proceed your url however if certification issues are there or not
            handler.proceed();
        }

    });
myWebView.loadUrl(url);

EDIT: if you want to enable javascript interface for your webview do the following procedure

private JavascriptInterface jsInterface;


engine.getSettings().setJavaScriptEnabled(true);
        jsInterface = new JavascriptInterface();
        try {
            ComponentName comp = new ComponentName(this, Dashboard.class);
            PackageInfo pinfo = getPackageManager().getPackageInfo(comp.getPackageName(), 0);
            jsInterface.versionCode = pinfo.versionCode;
            } catch (android.content.pm.PackageManager.NameNotFoundException e) {
            }
            engine.addJavascriptInterface(jsInterface, "androidlearnscripture");
                            engine.requestFocus(View.FOCUS_DOWN);
        }

And the javascript code will go here

    public void onPageStarted(WebView view, String url,Bitmap favicon) {
            jsInterface.enablePreferencesMenu = false;
            jsInterface.modalIsVisible = false;
            jsInterface.urlForSharing = null;
            bLoadingFinished = false;
}
like image 22
Bhavik Mehta Avatar answered Oct 27 '22 16:10

Bhavik Mehta