6

This is how I am calling the SMS app:

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 sending messages via twitter/Whatsapp/Facebook? What should I write in place of mms-sms? I found no documentation on such.

Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
Hick
  • 35,524
  • 46
  • 151
  • 243
  • Your Accept Score is really low. If you want people to answer your questions, it needs to be higher. – Ollie C Mar 14 '12 at 17:26

4 Answers4

8

I can't also find any way of calling Facebook/Twitter directly, but you could always call android.content.Intent.ACTION_SEND and let the user choose the 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, there might be a bug when using this to share through Facebook. For more information please see: Android Facebook Intent

Community
  • 1
  • 1
Telmo Marques
  • 5,066
  • 1
  • 24
  • 34
  • Tried this ,it is showing only email not showing direct message option ,other chat applications in dialog,how to show them. –  Oct 16 '13 at 07:23
2
    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();
   }  

    }
krishan
  • 1,141
  • 1
  • 12
  • 24
  • 1
    Does not check whether WhatsApp is installed or not. See http://stackoverflow.com/questions/15462874/sending-message-through-whatsapp – 0101100101 Jul 25 '14 at 14:29
  • I have edited my answer. now it checks whether WhatsApp is installed or not. – krishan Jan 20 '16 at 07:55
0
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.whatsapp");
i.putExtra("chat",true);
i.setType("text/plain");
startActivity(i);
user254875486
  • 11,190
  • 7
  • 36
  • 65
tarun
  • 1
  • 2
0

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 ;    
}
Varun
  • 1,938
  • 18
  • 19