Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android – How to check if webview is available?

Is there a way to check if webview is available on the device?

Background: If I add <uses-feature android:name="android.software.webview"/> to the Manifest the number of supported devices on Google Play drops from over 12,000 to less than 6,000 devices. So I added android:required="false" to this feature. In case webview is available websites should be displayed inside the app otherwise launched in the default browser:

String mUrl = "http://www.example.com";
if (*** WHAT TO PUT HERE? ***) {
    WebView webview = (WebView) findViewById(R.id.webView);
    if (Build.VERSION.SDK_INT >= 24) {
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.toString());
                return false;
            }
        });
    } else {
        webview.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
    }
    webview.loadUrl(mUrl);
} else {
    Uri uri = Uri.parse(mUrl);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}

Edit (to make things clear): <uses-permission android:name="android.permission.INTERNET" /> is (and always had been) part of the manifest. It’s just the addition of <uses-feature android:name="android.software.webview" /> which causes the drop of supported devices.

There is someone having the same issue here: https://issuetracker.google.com/issues/37035282 (unfortunately not answered)

like image 855
klabauter Avatar asked May 11 '17 13:05

klabauter


People also ask

How do I enable WebView on Android?

Scroll and find Android System WebView.Tap on it to open the app's info page. Tap "Enable." If you have previously disabled Android System WebView, the leftmost of the two large buttons on the app's info page will read Enable. Click on it to enable Android System WebView once more!

Is Android WebView deprecated?

The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.


2 Answers

Although @vsatkh pointed out that it is not necessarily needed to declare this feature as required, you can check the device’s feature compatibility as follows:

getPackageManager().hasSystemFeature("android.software.webview")

This method returns true or false.

Some additional information about Google Play’s filtering: Google Play only filters supported devices based on <uses-feature> elements declared in the manifest. <uses-permission> elements don’t affect Google Play’s filtering unless they imply a feature. android.permission.INTERNET does not imply any feature. Permissions that imply features are listed here: https://developer.android.com/guide/topics/manifest/uses-feature-element.html#permissions

like image 159
baruffa Avatar answered Sep 23 '22 16:09

baruffa


Alternative solution:
val hasWebView: Boolean = kotlin.runCatching { CookieManager.getInstance() }.isSuccess

It works because if WebView is not available then CookieManager.getInstance() will throw AndroidRuntimeException.

getPackageManager().hasSystemFeature("android.software.webview") is not reliable. It will return true if the device is supposed to have a webview, but has disabled it in system settings.

I tested it on a Huawei P20 by going to Settings -> Apps -> Android System WebView -> Disable

like image 29
Torkel Velure Avatar answered Sep 22 '22 16:09

Torkel Velure