要求地理定位时出现Android Webview错误 [英] Android webview error when asking for geolocation

查看:275
本文介绍了要求地理定位时出现Android Webview错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用getCurrentPosition来获取用户地理位置信息,并且在Google Chrome浏览器中工作正常,但是在Android Webview中却遇到此错误:

I'm getting the user geolocation with getCurrentPosition and it's working fine in Google Chrome, But in Android Webview i get this error:

E/cr_LocationProvider:注册时捕获到安全异常 系统中的位置更新.该应用程序没有 足够的地理位置许可. E/cr_LocationProvider: newErrorAvailable应用程序没有足够的地理位置 权限.

E/cr_LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions. E/cr_LocationProvider: newErrorAvailable application does not have sufficient geolocation permissions.

这是我的Manifast:

This is my Manifast:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="il.co.example.example">
<application
    android:allowBackup="true"
    android:icon="@mipmap/example_icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/example_icon_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

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

这是我的MainActivity: 包il.co.example.example;

And This is my MainActivity: package il.co.example.example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.GeolocationPermissions;
import android.webkit.GeolocationPermissions.Callback;
import android.webkit.WebSettings;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

class MyClient extends WebChromeClient {

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

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();

        // Enable Javascript
        webSettings.setJavaScriptEnabled(true);
        // Enable Geolocation
        webSettings.setGeolocationEnabled(true);

        // URL for Webview
        mWebView.loadUrl("https://www.example.com");

        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient());

        // HTML5 API flags
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setDatabaseEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);

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

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

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

我想念什么?谢谢

推荐答案

如果您的应用定位到 Android 5.0(API级别21)或更高版本,则必须声明您的应用使用了 android清单文件中的.hardware.location.network或android.hardware.location.gps 硬件功能,具体取决于您的应用是从NETWORK_PROVIDER还是从GPS_PROVIDER接收位置更新.

If your app targets Android 5.0 (API level 21) or higher, you must declare that your app uses the android.hardware.location.network or android.hardware.location.gps hardware feature in the manifest file, depending on whether your app receives location updates from NETWORK_PROVIDER or from GPS_PROVIDER.

如果您的应用程序从这两个位置提供者来源中接收到位置信息,则需要声明该应用程序在您的应用程序清单中使用了这些硬件功能.在运行Android 5.0(API 21)之前版本的设备上,请求ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限包括对位置硬件功能的隐式请求.但是,请求这些权限不会自动请求Android 5.0(API级别21)及更高版本上的位置硬件功能.

If your app receives location information from either of these location provider sources, you need to declare that the app uses these hardware features in your app manifest. On devices running versions prior to Android 5.0 (API 21), requesting the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission includes an implied request for location hardware features. However, requesting those permissions does not automatically request location hardware features on Android 5.0 (API level 21) and higher.

以下代码示例演示了如何在从设备GPS读取数据的应用的清单文件中声明权限和硬件功能:

The following code sample demonstrates how to declare the permission and hardware feature in the manifest file of an app that reads data from the device's GPS:

<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />  </manifest>

这篇关于要求地理定位时出现Android Webview错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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