Well, I've solved this problem, I do not know if it's the right one. Here is the code below.
In the onCreate method
webView = (WebView) findViewById(R.id.webview_site);
webView.setWebViewClient(new MyBrowser());
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new GeoWebChromeClient());
webView.getSettings().setGeolocationDatabasePath(getApplicationContext().getFilesDir().getPath());
dialog = new AlertDialog.Builder(this);
abrirPagina();
I created two classes
private class MyBrowser extends WebViewClient {
public boolean overrideURLLoading (WebView view, String url){
view.loadUrl(url);
return true;
}
}
/**
* A subclasse WebChromeClient lida com chamadas relacionadas à UI
*/
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(final String origin,
final GeolocationPermissions.Callback callback) {
//dialog perguntando se o usuário permite ou não pegar sua localização
dialog.setTitle("Acessar sua localização");
dialog.setMessage("O aplicativo quer acessar sua localização. Você" +
" permite?");
dialog.setCancelable(false);
dialog.setPositiveButton("Permitir", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
callback.invoke(origin, true, true);
Log.i("localizacao", origin);
}
});
dialog.setNegativeButton("Negar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
callback.invoke(origin, false, true);
Log.i("localizacao", origin);
}
});
dialog.create();
dialog.show();
}
}
And the method abrirPagina()
private void abrirPagina() {
try {
WebSettings ws = webView.getSettings();
if (detecta.existeConexao()) {
progressbar.setVisibility(View.INVISIBLE);
String url = "https://www.flaviodeoliveira.com.br";
ws.setAllowFileAccess(true);
ws.setGeolocationEnabled(true);
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
ws.setJavaScriptEnabled(true);
ws.setAppCacheMaxSize( 5 * 1024 * 1024 ); //5mb
ws.setSupportZoom(false);
ws.setAppCacheEnabled(true);
ws.setLoadsImagesAutomatically(true);
ws.setAppCacheEnabled(true);
ws.setDatabaseEnabled(true);
ws.setDomStorageEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl(url);
} else {
progressbar.setVisibility(View.VISIBLE);
ws.setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
Toast.makeText(this, "No momento você está sem conexão com a internet.",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
Running the app, it will display a dialog
asking whether to allow or deny to get the location by the browser within the webview. If it allows, the second time it runs the app, the dialog
will no longer appear because in callback.invoke(origin, true, true);
I left the last parameter as true
so that it will memorize.
Well I do not know if it's the best way, however from what I've read it worked in the app.