Open WhatsApp from webview android studio

1

Hello everyone and thank you very much for your time. Before writing my question I came up with possible solutions, I researched and could not find a solution.

I have a WebView in Android Studio in which I wish through the WhatsApp api ( link .) open, worth the redundancy, WhatsApp ... but I can not, I do not achieve any successful result.

It should be noted that for example it does work for me to use the aforementioned api in a common browser such as Chrome. The question / s is / are: do I have to make modifications to the WebView code? Do I need special permits? What is escaping me?

Please, if someone can help me, I would be very grateful. Again, thank you for your time in reading my problem.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="UnClick"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:configChanges="screenSize|orientation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Autos" />
    <activity android:name=".Motos" />
    <activity android:name=".Empleos" />
    <activity android:name=".ParaComer" />
    <activity android:name=".Hogar" />
    <activity android:name=".SaludYBelleza" />
    <activity android:name=".Telefonos" />
    <activity android:name=".CampoyMascotas" />
    <activity android:name=".Alquileres" />
    <activity android:name=".Corralon" />
</application>

That's the Manifest

package appsmate.com.unclick;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;



public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private String url_Ini = "http://unclickcapilla.esy.es/";
    private String url_Almacen = "http://unclickcapilla.esy.es/almacen.html";
    private String url_Carniceria = "http://unclickcapilla.esy.es/carniceria.html";
    private String url_Verduleria = "http://unclickcapilla.esy.es/verduleria.html";
    private String url_Indumentaria = "http://unclickcapilla.esy.es/indumentaria.html";
    private String url_Resto = "http://unclickcapilla.esy.es/restoranparrillas.html";
    private String url_Delivery = "http://unclickcapilla.esy.es/delivery.html";




    private ProgressDialog mProgressDialog;
    private WebView webviewsi;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);



        mProgressDialog = new ProgressDialog(this);
        webviewsi = (WebView) findViewById(R.id.webviewsi);
        WebSettings websetting = webviewsi.getSettings();
        websetting.setJavaScriptEnabled(true);
        websetting.setSupportZoom(true);
        websetting.setDisplayZoomControls(false);


        webviewsi.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
                try {
                    webView.stopLoading();
                } catch (Exception e) {
                }

                if (webviewsi.canGoBack()) {
                    webviewsi.goBack();

                }

                webView.loadUrl("about:blank");
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Error");
                alertDialog.setMessage("Necesitamos estar conectados a una red para mostrarte el contenido ... :(");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "De nuevo", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                        startActivity(getIntent());
                    }
                });

                alertDialog.show();
                super.onReceivedError(webView, errorCode, description, failingUrl);

            }
        });





        webviewsi.setWebChromeClient(new WebChromeClient() {


            @Override


            public void onProgressChanged(WebView view, int progress) {
                mProgressDialog.setTitle("Un momento...");
                mProgressDialog.setMessage("Solicitando contenido a la base de datos ...");
                mProgressDialog.setProgress(0);
                mProgressDialog.show();
                MainActivity.this.setProgress(progress * 1000);
                mProgressDialog.incrementProgressBy(progress);
                if (progress == 100) {
                    mProgressDialog.dismiss();
                }
            }
        });

        webviewsi.loadUrl(url_Ini);

    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }




    @Override

    public boolean onNavigationItemSelected(MenuItem item) {



        int id = item.getItemId();

        if (id == R.id.portada) {
            webviewsi.getSettings();
            webviewsi.loadUrl(url_Ini);

        }



        if (id == R.id.almacen) {
            webviewsi.getSettings();
            webviewsi.loadUrl(url_Almacen);

        }
        if (id == R.id.caniceria) {
            webviewsi.getSettings();
            webviewsi.loadUrl(url_Carniceria);
        }
        if (id == R.id.verduleria) {
            webviewsi.getSettings();
            webviewsi.loadUrl(url_Verduleria);
        }
        if (id == R.id.indumentaria) {
            webviewsi.getSettings();
            webviewsi.loadUrl(url_Indumentaria);
        }
        if (id == R.id.resto) {
            webviewsi.getSettings();
            webviewsi.loadUrl(url_Resto);
        }
        if (id == R.id.delivery) {
            webviewsi.getSettings();
            webviewsi.loadUrl(url_Delivery);
        }





        if (id == R.id.autos) {

            Intent intent = new Intent (this, Autos.class);
            startActivity(intent);


        } else if (id == R.id.motos) {
            Intent intent = new Intent (this, Motos.class);
            startActivity(intent);

        } else if (id == R.id.nav_empleos) {
            Intent intent = new Intent (this, Empleos.class);
            startActivity(intent);
        }
        else if (id == R.id.comidas)
        {
            Intent intent = new Intent (this, ParaComer.class);
            startActivity(intent);
        }



        else if (id == R.id.hogar) {
            Intent intent = new Intent (this, Hogar.class);
            startActivity(intent);


        } else if (id == R.id.saludybelleza) {
            Intent intent = new Intent (this, SaludYBelleza.class);
            startActivity(intent);
        }

        else if (id == R.id.telefonia) {
            Intent intent = new Intent (this, Telefonos.class);
            startActivity(intent);
        }


        else if (id == R.id.campoymascotas) {
            Intent intent = new Intent (this, CampoyMascotas.class);
            startActivity(intent);
        }


        else if (id == R.id.alquileres) {
            Intent intent = new Intent (this, Alquileres.class);
            startActivity(intent);
        }

        else if (id == R.id.corralon) {
            Intent intent = new Intent (this, Corralon.class);
            startActivity(intent);


        }



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;



    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webviewsi.canGoBack()) {
            webviewsi.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }



}

And this is the MainActivity

    
asked by Emanuel Biancotti 07.08.2018 в 21:54
source

1 answer

0

An option which does not require permissions is to use the api to open Whatsapp from a web page that is loaded within a WebView .

For this you need to define a WebChromeClient that will cause you to open the application, in the page that you would load in the WebView you must have defined the scheme of Whatsapp defining the phone number with the "area code", "area code" or whatever it is called in your country, for example:

<a href="https://api.whatsapp.com/send?phone=5212345678910&text=Hola,%20soyI%20Emanuel%20Biancotti" class="whatsapplink hideweb" style="background-color:#2DC100">Enviar mensaje.</a>

This would be the code:

    WebView webView = (WebView)findViewById(R.id.myWebView);      

    //Página web conteniendo el código para abrir Whatsapp.
    String url = "http://mydomain/webpage.html";        

    webView.getSettings().setJavaScriptEnabled(true);
    //Define WebChromeClient() para el WebView.
    webView.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onReceivedTitle(WebView view, String title) {
            getWindow().setTitle("Open Whatsapp by Jorgesys");
        }
    });

This is an example of what I am saying, the blue area contains a WebView that loads a page that contains the link to open the contact:

Obviously your application must have the permission to access the internet, necessary to load the web page within WebView .

<uses-permission android:name="android.permission.INTERNET"/>
    
answered by 07.08.2018 / 22:35
source