位置是在Chrome中访问的,在WebView中不起作用 [英] Location is accessed in Chrome, doesn't work in WebView

查看:112
本文介绍了位置是在Chrome中访问的,在WebView中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebView,该WebView打开一个URL,该URL需要访问用户的位置.在应用程序外部使用Google Chrome浏览器时,它可以确定位置,但是在应用程序中,它表示我不允许应用程序使用位置.目前,我已添加

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.

推荐答案

您只需要导入android.webkit.GeolocationPermissions.Callbackandroid.webkit.WebChromeClient 设置提供的来源的地理位置许可状态 ,方法如下:通过WebChromeClientonGeolocationPermissionsShowPrompt方法调用callback.invoke(origin, true, false);

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

代码

      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");

,您必须添加2个清单权限为

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" />

希望它能起作用.

这篇关于位置是在Chrome中访问的,在WebView中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆