URL is with Location how to open in Webview Android

Asked

Viewed 796 times

1

People at the url link I put in the webview has implemented localization. As soon as the user opens the site by Chrome or any browser appears a dialog asking if it allows to use the location.

But in webview does not appear this pop-up, have to implement this, appear this dialog within the app?

Quando entro no site pelo navegador é esse dialog que aparece

  • but it opens the said site in the webview that runs on tel?

  • @Armando he opens the link normally in the app, works normally. I just wanted to appear this pop up also in the app

  • problem that the webview is something like a meta-browser, and this control comes from the device and the ones installed on it, I advise in case that link you really need to show this popup via html: https://developer.chrome.com/multidevice/webview/overview

  • otherwise, Voce can create something to check if the app accessed the gps and after confirming open the page

1 answer

1

Good staff I solved as follows, I do not know if it is the most correct. It follows below the code.

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 present a dialog asking whether to allow or deny to pick up the location by the browser within the webview. If it allows, the second time it runs the app will no longer appear the dialog for in the callback.invoke(origin, true, true); left the last parameter as true for him to memorize.

Well I don’t know if it’s the best way, but from what I read it worked in the app.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.