The WebView cannot be located.

Source: Internet
Author: User

The WebView cannot be located.
I encountered a problem that cannot be located in WebView. I was unable to locate the problem when using webview. I found someone said they would do this.

        WebSettings settings = wbContent.getSettings();        settings.setJavaScriptEnabled(true);        settings.setJavaScriptCanOpenWindowsAutomatically(true);        settings.setGeolocationEnabled(true);        settings.setDomStorageEnabled(true);        settings.setDatabaseEnabled(true);        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();        settings.setGeolocationDatabasePath(dir);        webChromeClient = new WebChromeClient(){            @Override            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {                callback.invoke(origin, true, true);                super.onGeolocationPermissionsShowPrompt(origin, callback);            }        };        wbContent.setWebChromeClient(webChromeClient);        wbContent.loadUrl("https://xxxxxxxxxxxxxxxxxxxxxxxx");

In many places on the internet, after this code is added, you can locate it normally. However, after I add it, there is still no use. No way, Baidu is not powerful to Google.
I read the log and said that I didn't get the permission, but callback. invoke (origin, true, true) in the Code is an operation to get the positioning permission.
I thought about it and suddenly thought that I had to dynamically apply for permissions after 6.0.
Code for dynamically applying for Permissions

if (Build.VERSION.SDK_INT >= 23) {            int checkPermission = ContextCompat.checkSelfPermission(LocationTestActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);            if (checkPermission != PackageManager.PERMISSION_GRANTED) {                ActivityCompat.requestPermissions(LocationTestActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);                ActivityCompat.requestPermissions(LocationTestActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);            }else {                wbContent.loadUrl("https://xxxxxxxxxxxxxxxxxxxxxxxx");            }        }

In this way, you can get the Positioning Result normally. Some people may not know where to apply dynamically. In fact, this depends on your specific process, you can also apply when you jump to this page or when displaying the page.

Finally, the onGeolocationPermissionsShowPrompt method is called only once. It is called only for the first time, just like the dynamic permission application.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.