How to use whatsapp from my android app?

This is how I call the SMS application:

Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", "The SMS text"); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent); 

How do I do the same for posting via Twitter / Whatsapp / Facebook? What should I write instead of mms-sms? I did not find such documentation.

+6
source share
4 answers

I also cannot find a way to directly call Facebook / Twitter, but you can always call android.content.Intent.ACTION_SEND and let the user select an application.

Android ACTION_SEND intent

 Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(Intent.EXTRA_SUBJECT, "Subject"); i.putExtra(Intent.EXTRA_TEXT, "Message body"); startActivity(Intent.createChooser(i, "Share dialog title")); 

However, using this to share via Facebook may result in an error. For more information, see Android Facebook Intention

+8
source
  public void onClickWhatsApp(View view) { try { Intent waIntent = new Intent(Intent.ACTION_SEND); waIntent.setType("text/plain"); String text = "YOUR TEXT HERE"; waIntent.setPackage("com.whatsapp"); if (waIntent != null) { waIntent.putExtra(Intent.EXTRA_TEXT, text);// startActivity(Intent.createChooser(waIntent, "Share with")); } else { Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) .show(); }} catch (NameNotFoundException e) { Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) .show(); } } 
+2
source
 Intent i = new Intent(Intent.ACTION_SEND); i.setPackage("com.whatsapp"); i.putExtra("chat",true); i.setType("text/plain"); startActivity(i); 
0
source

You can use the following snippets: For WhatsApp:

 public void onClickWhatsApp(View view) { PackageManager pm=getPackageManager(); try { Intent waIntent = new Intent(Intent.ACTION_SEND); waIntent.setType("text/plain"); String text = "YOUR TEXT HERE"; PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA); //Check if package exists or not. If not then code //in catch block will be called waIntent.setPackage("com.whatsapp"); waIntent.putExtra(Intent.EXTRA_TEXT, text); startActivity(Intent.createChooser(waIntent, "Share with")); } catch (NameNotFoundException e) { Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) .show(); } } 

For Twitter:

 void shareOnTwitter() { PackageManager pm=getPackageManager(); try { Intent waIntent = new Intent(Intent.ACTION_SEND); waIntent.setType("text/plain"); String text = "Insert Tweet Here"; @SuppressWarnings("unused") PackageInfo info=pm.getPackageInfo("com.twitter.android", PackageManager.GET_META_DATA); //Check if package exists or not. If not then code //in catch block will be called waIntent.setPackage("com.twitter.android"); waIntent.putExtra(Intent.EXTRA_TEXT, text); startActivity(Intent.createChooser(waIntent, "Share with")); } catch (NameNotFoundException e) { Toast.makeText(this, "Twitter not Installed", Toast.LENGTH_SHORT) .show(); return ; } return ; } 
0
source

Source: https://habr.com/ru/post/910728/


All Articles