地理位置权限不足

我正在开发一个Android项目。 基本上,我试图运行显示谷歌地图的WebView。

我想显示我自己的位置,但是当我按下这个button,我得到的例外,如:

11-18 17:29:56.237 7678-7678/com.example.ali.bellsolution E/LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions. 11-18 17:29:56.257 7678-7678/com.example.ali.bellsolution E/LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions. 11-18 17:29:56.260 7678-7678/com.example.ali.bellsolution E/LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions. 

我已经将这些权限添加到清单文件中:

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

MainActivity.java

 package com.example.ali.bellsolution; import android.Manifest; import android.app.Activity; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.GeolocationPermissions; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { private static final int REQUEST_FINE_LOCATION = 0; WebView myWebView; String mapPath = "https://www.google.co.uk/maps/@51.5112044,-0.2712415,14z"; /** * WebViewClient subclass loads all hyperlinks in the existing WebView */ public class GeoWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // When user clicks a hyperlink, load in the existing WebView view.loadUrl(url); return true; } } /** * WebChromeClient subclass handles UI-related calls * Note: think chrome as in decoration, not the Chrome browser */ public class GeoWebChromeClient extends WebChromeClient { @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { // Always grant permission since the app itself requires location // permission and the user has therefore already granted it callback.invoke(origin, true, false); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, REQUEST_FINE_LOCATION); setContentView(R.layout.activity_main); myWebView = (WebView) this.findViewById(R.id.mapview); myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); myWebView.getSettings().setBuiltInZoomControls(true); myWebView.setWebViewClient(new GeoWebViewClient()); myWebView.getSettings().setJavaScriptEnabled(true); myWebView.getSettings().setGeolocationEnabled(true); myWebView.setWebChromeClient(new GeoWebChromeClient()); myWebView.loadUrl(mapPath); } @Override public void onBackPressed() { // Pop the browser back stack or exit the activity if (myWebView.canGoBack()) { myWebView.goBack(); } else { super.onBackPressed(); } } private void loadPermissions(String perm, int requestCode) { if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) { if (!ActivityCompat.shouldShowRequestPermissionRationale(this, perm)) { ActivityCompat.requestPermissions(this, new String[]{perm}, requestCode); } } } } 

performance:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ali.bellsolution" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="WebViewApp" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="WebViewApp" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

Solutions Collecting From Web of "地理位置权限不足"

您可能需要在需要时向用户请求地理位置权限,但在调用Callback之前等待用户的响应:

 private static final int REQUEST_FINE_LOCATION = 1; private String mGeolocationOrigin; private GeolocationPermissions.Callback mGeolocationCallback; public class GeoWebChromeClient extends WebChromeClient { @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { // Geolocation permissions coming from this app's Manifest will only be valid for devices with // API_VERSION < 23. On API 23 and above, we must check for permissions, and possibly // ask for them. String perm = Manifest.permission.ACCESS_FINE_LOCATION; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(MainActivity.this, perm) == PackageManager.PERMISSION_GRANTED) { // we're on SDK < 23 OR user has already granted permission callback.invoke(origin, true, false); } else { if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, perm)) { // ask the user for permission ActivityCompat.requestPermissions(MainActivity.this, new String[] {perm}, REQUEST_FINE_LOCATION); // we will use these when user responds mGeolocationOrigin = origin; mGeolocationCallback = callback; } } } 

并接收许可请求的结果:

 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQUEST_FINE_LOCATION: boolean allow = false; if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // user has allowed this permission allow = true; } if (mGeolocationCallback != null) { // call back to web chrome client mGeolocationCallback.invoke(mGeolocationOrigin, allow, false); } break; } } 

这些Android 6用户的修补程序是在您的代码中的以下位置:

 if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 2); } 

这是必需的,因为Android 6要求您提示用户一定的权限。