[英]Does not have sufficient Geolocation Permissions Android Webview
晚上好。 我正在使用Webview在Android上制作跟踪应用程序。 我遇到了问题,因为系统在该位置旁边都运行良好。 我尝试过Grishma Ukani先生的一些方法(非常感谢)。
package com.example.webview;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* A minimal WebView app with HTML5 geolocation capability
*
* @author David M. Chandler
*/
public class GeoWebViewActivity extends Activity {
/**
* 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);
}
}
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView1);
// Brower niceties -- pinch / zoom, follow links in place
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setWebViewClient(new GeoWebViewClient());
// Below required for geolocation
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.setWebChromeClient(new GeoWebChromeClient());
// Load google.com
mWebView.loadUrl("http://www.google.com");
}
@Override
public void onBackPressed() {
// Pop the browser back stack or exit the activity
if (mWebView.canGoBack()) {
mWebView.goBack();
}
else {
super.onBackPressed();
}
}
}
一切顺利,没有错误。 但仍然无法获得设备位置/权限然后此信息出现在android studio上
E / cr_LocationProvider:在从系统注册位置更新时捕获安全性异常。 该应用程序没有足够的地理定位权限。 E / cr_LocationProvider:newErrorAvailable应用程序没有足够的地理定位权限。
然后我试图修改与地理位置权限不足的链接,并将此修改放在我的公共类GeoWebViewActivity extends Activity
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(GeoWebViewActivity.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(GeoWebViewActivity.this, perm)) {
// ask the user for permission
ActivityCompat.requestPermissions(GeoWebViewActivity.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;
}
}
}
但有一些错误:
任何对另一种方法的帮助和建议都将非常感激。
您需要导入NonNull
注释。
添加import javax.annotation.Nonnull;
对于您的导入,您可以在课程顶部找到它们。
你的类可能在一个包中,你需要在GeoWebChromeClient
类中声明该包。 请注意,您有一个行package com.example.webview;
在GeoWebViewActivity
类的顶部,指示此类位于../com/example/webview
。 如果GeoWebChromeClient
不再是内部类,则需要类似的包行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.