Location is accessed in Chrome, doesn't work in WebView
Asked Answered
N

2

6

I have a WebView that opens a URL that requires access to the user's location. It can determine the location when using Google Chrome outside the app, but in the app, it says I am not allowing the application to use location. Currently I have added

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

to my Manifest, and in my Class, I have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webViewActivity);
    WebView webView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());
    webSettings.setAllowUniversalAccessFromFileURLs(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setGeolocationEnabled(true)

Am I missing something? Thank you for your help.

Nonsmoker answered 25/1, 2016 at 6:31 Comment(1)
refer https://mcmap.net/q/193363/-android-webview-geolocationPustulant
K
10

You have to import android.webkit.GeolocationPermissions.Callback and android.webkit.WebChromeClient just to Set the Geolocation permission state for the supplied origin by calling callback.invoke(origin, true, false); in onGeolocationPermissionsShowPrompt method of WebChromeClient like below

Code

      mWebView = (WebView) findViewById(R.id.mywebview);
      WebSettings webSettings = mWebView.getSettings();
      webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }
        });

        webSettings.setJavaScriptEnabled(true);
        webSettings.setGeolocationEnabled(true);

        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin,
                    Callback callback) {

                callback.invoke(origin, true, false);
            }
        });

        mWebView.loadUrl("http://www.google.com");

and you have to add 2 manifest permission's as

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Hope it works.

Knotty answered 25/1, 2016 at 10:17 Comment(2)
its working for nougut but not for marshmallow ! please helpBerar
get runtime permission for marshmallowAlumna
C
-1

@Kapil Rajput 's Kotlin version that worked for me:

Code

class MainActivity : AppCompatActivity() {
    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val url = "your-application"
        val webView = findViewById<WebView>(R.id.web)

        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return true
            }
        }

        webView.settings.userAgentString = "https.agent"
        webView.settings.javaScriptEnabled = true
        webView.settings.javaScriptCanOpenWindowsAutomatically = true
        webView.settings.setGeolocationEnabled(true)

        webView.webChromeClient = object : WebChromeClient() {
            override fun onGeolocationPermissionsShowPrompt(origin: String?, callback: GeolocationPermissions.Callback?) {
                callback!!.invoke(origin, true, false);
            }
        }

        webView.loadUrl(url)
    }
Calotte answered 21/8, 2022 at 2:23 Comment(1)
Not working at all, I guess you have an error in the "callback!!.invoke(origin, true, false);"Reviviscence

© 2022 - 2024 — McMap. All rights reserved.