android send intent to specific contact (whatsapp, sms, hike, etc.)

3.1k Views Asked by At

I have implemented an app in which I can pick a contact and list some custom information about this contact in a tablelayout. In each row of this tablelayout I have a column (button) which should send a message to the specific user using sms, whatapp, hike, etc. On click event, the Action_Send intent should open but instead having to pick the contact I want to put the contact or contact id as an extra so that I do not have to choose the contact from the list in whatsapp/sms/hike intent. Is this possible or do I have to implement the send to picker on my own and start a specific intent for each app?

Cheers Magnus

2

There are 2 best solutions below

0
On BEST ANSWER

You have to implement the send action for different messaging apps like hike whatsapp hike etc. as you can't tell these apps to send a message to a specific contact as the database is protected.

Reason

  1. You never know which contact has what id in these apps like for you what may be "foo" can be something else in whatsapp database.

So all you have to do this is just put data and package name.

Hike - com.bsb.hike
Whatsapp - com.whatsapp

Intent i=new Intent();
i.setPackageName("");
startActivity(i);

Hope it helps.

1
On

I have been using this:

// Define a button on the screen
final AppCompatButton sendWhatsappText = findViewById(R.id.btnWhatsappText);

// Associate the onClickListener with the button
sendWhatsappText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        sendWhatsappMessage(phoneNo);
    }
});

// The actual method to send message to a contact defined by the phone number
private void sendWhatsappMessage(String phoneNo) {
    String url = "https://api.whatsapp.com/send?phone=" + phoneNo + "&text=TYPE YOUR MESSAGE";
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    intent.setPackage(packageName);

    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        System.out.println("Error Message");
    }
}