Android要求获得在webview中使用位置的权限 [英] Android Ask for permission to use location within webview

查看:206
本文介绍了Android要求获得在webview中使用位置的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用webview的应用程序,在webview中是一张地图,我让它为用户自动找到用户位置,使用以下代码:

I have an application that uses a webview and inside the webview is a map, I have got it working to the user automatically find the users location with the following code:

清单文件权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

WebView活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);

    WebViewSettings();

    LoadWebPage(urlString);
}

WebView设置:

@SuppressLint("SetJavaScriptEnabled")
public void WebViewSettings(){

    webView = (WebView) findViewById(R.id.webview);
    webView.canGoBack();

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.setWebViewClient(new WebViewClientSetup());
    webView.setWebChromeClient(new GeoWebChromeClient());
    webView.getSettings().setGeolocationEnabled(true);

    myTimer = new Timer();
    //Start this timer when you create you task
    myTimer.schedule(new loaderTask(), 20000);

}

WebViewClient:

public class WebViewClientSetup extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        // Check to see if there is a progress dialog
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(context);
            progressDialog.setTitle("Loading...");
            progressDialog.setMessage("Please wait.");
            progressDialog.setCancelable(true);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler,
            SslError error) {
        handler.proceed();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        isPageLoadedComplete = true;
        View mapHeight = (View) findViewById(R.id.webview);
        int height = mapHeight.getHeight();
        Log.d("map height", "map height" + height);
        // Page is done loading;
        // hide the progress dialog and show the webview
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCod,
            String description, String failingUrl) {
        AlertDialog();
    }
}

WebChromeClient:

public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
    }
}

所以这项工作正常但它会自动获取用户如果应用程序可以使用位置服务,则不会提示用户。

So this is working but it automatically gets the users' location without prompting the user if the app can use location services.

有没有办法将此功能添加到应用程序?谢谢

Is there a way to add this functionality to the app? Thanks

推荐答案

那是因为你需要创建提示。只需调用 onGeolocationPermissionsShowPrompt 函数即可让您知道网页是否需要该位置。

That's because you need to create the prompt. The onGeolocationPermissionsShowPrompt function is simply called to let you know the webpage wants the location.

callback.invoke(origin,true,false); 是告诉WebView可以使用该位置的响应。

callback.invoke(origin, true, false); is the response telling the WebView it's OK to use the location.

最后一个参数是是否记住此设置。请参阅: http://developer.android.com/reference/android/webkit /GeolocationPermissions.Callback.html

The last argument is whether or not to remember this setting. See: http://developer.android.com/reference/android/webkit/GeolocationPermissions.Callback.html

示例:

AlertDialog.Builder adb = new AlertDialog.Builder(context);
..... //set up title, message, etc
adb.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        callback.invoke(origin, false, false);
    }
});
adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        callback.invoke(origin, true, false);
    }
});
adb.show();

这篇关于Android要求获得在webview中使用位置的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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