Integrating WhatsApp with Android Application using Intent (Java/Kotlin)


To integrate WhatsApp with an Android application using Intent, you can use the following code.



Java Example

    To Send a message to a specific phone number:

    Intent sendIntent = new Intent("android.intent.action.SEND");
    sendIntent.setType("text/plain");
    sendIntent.putExtra("android.intent.extra.TEXT", "Your message here");
    sendIntent.putExtra("jid", "PHONE_NUMBER_HERE");
    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);

    To share a file:

    Intent shareIntent = new Intent("android.intent.action.SEND");
    shareIntent.setType("image/jpeg"); // Or any other file type
    shareIntent.putExtra("android.intent.extra.STREAM", Uri.parse("file:///path/to/your/file.jpg"));
    shareIntent.putExtra("jid", "PHONE_NUMBER_HERE");
    shareIntent.setPackage("com.whatsapp");
    startActivity(shareIntent);


Using Kotlin:

    To Send a message to a specific phone number:

    val sendIntent = Intent("android.intent.action.SEND")
    sendIntent.type = "text/plain"
    sendIntent.putExtra("android.intent.extra.TEXT", "Your message here")
    sendIntent.putExtra("jid", "PHONE_NUMBER_HERE")
    sendIntent.setPackage("com.whatsapp")
    startActivity(sendIntent)

    To share a file:

    val shareIntent = Intent("android.intent.action.SEND")
    shareIntent.type = "image/jpeg" // Or any other file type
    shareIntent.putExtra("android.intent.extra.STREAM", Uri.parse("file:///path/to/your/file.jpg"))
    shareIntent.putExtra("jid", "PHONE_NUMBER_HERE")
    shareIntent.setPackage("com.whatsapp")
    startActivity(shareIntent)


Note:

  • Replace PHONE_NUMBER_HERE with the actual phone number of the WhatsApp contact you want to send the message to.

  • The above code will only work if the user has WhatsApp installed on their device.
  • The jid extra is used to specify the phone number of the WhatsApp contact.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap