How to open WhatsApp using an Intent in your Android App
Using the 2018 api:
String url = "https://api.whatsapp.com/send?phone="+number;Intent i = new Intent(Intent.ACTION_VIEW);i.setData(Uri.parse(url));startActivity(i);
This code working for me
String contact = "+00 9876543210"; // use country code with your phone number String url = "https://api.whatsapp.com/send?phone=" + contact; try { PackageManager pm = context.getPackageManager(); pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES); Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); } catch (PackageManager.NameNotFoundException e) { Toast.makeText(MainActivity.activity, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show(); e.printStackTrace(); }
This works perfectly 2021
Expansion of short forms:
numero = number phone
mensaje= message to send
private void openWhatsApp(String numero,String mensaje){ try{ PackageManager packageManager = getActivity().getPackageManager(); Intent i = new Intent(Intent.ACTION_VIEW); String url = "https://api.whatsapp.com/send?phone="+ numero +"&text=" + URLEncoder.encode(mensaje, "UTF-8"); i.setPackage("com.whatsapp"); i.setData(Uri.parse(url)); if (i.resolveActivity(packageManager) != null) { startActivity(i); }else { KToast.errorToast(getActivity(), getString(R.string.no_whatsapp), Gravity.BOTTOM, KToast.LENGTH_SHORT); } } catch(Exception e) { Log.e("ERROR WHATSAPP",e.toString()); KToast.errorToast(getActivity(), getString(R.string.no_whatsapp), Gravity.BOTTOM, KToast.LENGTH_SHORT); }}
Hope, This Helps!