问题
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:
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:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
WebViewSettings();
LoadWebPage(urlString);
}
WebView Settings:
@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:
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:
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
回答1:
That's because you need to create the prompt. The onGeolocationPermissionsShowPrompt
function is simply called to let you know the webpage wants the location.
callback.invoke(origin, true, false);
is the response telling the WebView it's OK to use the location.
The last argument is whether or not to remember this setting. See: 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();
来源:https://stackoverflow.com/questions/23498523/android-ask-for-permission-to-use-location-within-webview