[英]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.