[英]Android Ask for permission to use location within webview
I have an application that uses a webview and inside the webview is a map, I have got it working to the user automatically find the users location with the following code: 我有一个使用webview的应用程序,并且webview内有一个地图,我可以使用以下代码来让用户自动找到用户的位置:
Manifest file Permissions: 清单文件权限:
<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" />
WebView Activity: WebView活动:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
WebViewSettings();
LoadWebPage(urlString);
}
WebView Settings: WebView设置:
@SuppressLint("SetJavaScriptEnabled")
public void WebViewSettings(){
webView = (WebView) findViewById(R.id.webview);
webView.canGoBack();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClientSetup());
webView.setWebChromeClient(new GeoWebChromeClient());
webView.getSettings().setGeolocationEnabled(true);
myTimer = new Timer();
//Start this timer when you create you task
myTimer.schedule(new loaderTask(), 20000);
}
WebViewClient: WebViewClient:
public class WebViewClientSetup extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onLoadResource(WebView view, String url) {
// Check to see if there is a progress dialog
if (progressDialog == null) {
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Loading...");
progressDialog.setMessage("Please wait.");
progressDialog.setCancelable(true);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
handler.proceed();
}
@Override
public void onPageFinished(WebView view, String url) {
isPageLoadedComplete = true;
View mapHeight = (View) findViewById(R.id.webview);
int height = mapHeight.getHeight();
Log.d("map height", "map height" + height);
// Page is done loading;
// hide the progress dialog and show the webview
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
@Override
public void onReceivedError(WebView view, int errorCod,
String description, String failingUrl) {
AlertDialog();
}
}
WebChromeClient: WebChromeClient:
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
}
So this is working but it automatically gets the users' location without prompting the user if the app can use location services. 因此,这是可行的,但是它会自动获取用户的位置,而不会提示用户该应用程序是否可以使用位置服务。
Is there a way to add this functionality to the app? 有没有办法将此功能添加到应用程序? Thanks
谢谢
That's because you need to create the prompt. 那是因为您需要创建提示。 The
onGeolocationPermissionsShowPrompt
function is simply called to let you know the webpage wants the location. 简单地调用
onGeolocationPermissionsShowPrompt
函数可让您知道网页需要该位置。
callback.invoke(origin, true, false);
is the response telling the WebView it's OK to use the location. 是告诉WebView可以使用该位置的响应。
The last argument is whether or not to remember this setting. 最后一个参数是是否要记住此设置。 See: http://developer.android.com/reference/android/webkit/GeolocationPermissions.Callback.html
参见: http : //developer.android.com/reference/android/webkit/GeolocationPermissions.Callback.html
Example: 例:
AlertDialog.Builder adb = new AlertDialog.Builder(context);
..... //set up title, message, etc
adb.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.invoke(origin, false, false);
}
});
adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.invoke(origin, true, false);
}
});
adb.show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.