Android:使用 html5 使用 javascript api 确定 webview 中的

Android: Using html5 to determine geolocation in webview with javascript api(Android:使用 html5 使用 javascript api 确定 webview 中的地理位置)

本文介绍了Android:使用 html5 使用 javascript api 确定 webview 中的地理位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在 web 视图中遇到地理定位问题.我有一个网络应用程序.我目前没有使用 phonegap 或任何其他移动框架.我无法让内置的 html5 地理定位 javascript api 用于在 android 应用程序的 webview 中运行的应用程序.该网站在 android 2.0+(支持地理位置)上的 chrome 浏览器上运行良好.

I'm currently having an issue with geolocation in a webview. I have a webapp. I'm currently not using phonegap or any other mobile framework. I've been unsuccessful at getting the built-in html5 geolocation javascript api to work on an application that runs in a webview in an android app. The site works fine otherwise from the chrome browser on android 2.0+ (geolocation supported).

我正在针对 android api 版本 5 进行编译.

I'm compiling against android api version 5.

我读过这篇文章已经

Phonegap 的解决方案是编写一个封装内置调用并使用主机活动的代理,但我更喜欢在不使用电话间隙的情况下使用内置到 webview (webkit).

Phonegap's solution of writing a proxy which wraps the built in call and uses the host activity instead is good, but I'd prefer to use the built in to the webview (webkit) without using phone gap.

我已经在清单文件中设置了适当的权限:

I've set the proper permissions in the manifest file:

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

这是一个示例代码片段:

Here is an example code snippet:

webview = (WebView) findViewById(R.id.webview);
pbarDialog = new ProgressDialog(this);
pbarDialog.setCancelable(false);
pbarDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new MyChromeWebViewClient());
webview.setVerticalScrollBarEnabled(false);
WebSettings webSettings = webview.getSettings();
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setGeolocationEnabled(true);

...

private class MyChromeWebViewClient extends WebChromeClient {

@Override
public void onProgressChanged(WebView view, int progress) {
    // Activities and WebViews measure progress with different scales.
    // The progress meter will automatically disappear when we reach 100%
    activity.setProgress(progress * 100);
}

@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
    Log.d(LOG_TAG, message);
    // This shows the dialog box.  This can be commented out for dev
    AlertDialog.Builder alertBldr = new AlertDialog.Builder(activity);
    alertBldr.setMessage(message);
    alertBldr.setTitle("Alert");
    alertBldr.show();
    result.confirm();
    return true;
  }

}

private class MyWebViewClient extends WebViewClient {

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

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

有没有其他人在让 web 应用程序在 webview 中工作时遇到问题?

Has anyone else had issues with getting a web application to work in the webview?

推荐答案

onGeolocationPermissionsShowPrompt() 添加到 MyChromeWebViewClient 如下:

Add onGeolocationPermissionsShowPrompt() to MyChromeWebViewClient as below:

private class MyChromeWebViewClient extends WebChromeClient {

    @Override
    public void onProgressChanged(WebView view, int progress) {
        // Activities and WebViews measure progress with different scales.
        // The progress meter will automatically disappear when we reach 100%
        activity.setProgress(progress * 100);
    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        Log.d(LOG_TAG, message);
        // This shows the dialog box.  This can be commented out for dev
        AlertDialog.Builder alertBldr = new AlertDialog.Builder(activity);
        alertBldr.setMessage(message);
        alertBldr.setTitle("Alert");
        alertBldr.show();
        result.confirm();
        return true;
    }

    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
    }
}

您需要导入android.webkit.GeolocationPermissions".

添加此权限:

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

我猜这会奏效.

这篇关于Android:使用 html5 使用 javascript api 确定 webview 中的地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android:使用 html5 使用 javascript api 确定 webview 中的

基础教程推荐