0

I have a code that sends sms to a Whatsapp number. However, whenever I send a message, it opens Whatsapp. How will I be able to send an sms without opening the Whatsapp app?

String smsNumber = "9230......."; 
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net");  
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
CoderUni
  • 5,474
  • 7
  • 26
  • 58

2 Answers2

1

You can not do this, Because WhatsApp does not offer ContentProviders. So the legal way of doing things in Android is to use Intents.

But, if you really want to send messages to WhatsApp and automate it, I guess you can create AccessibilityService which will focus on events for WhatsApp package, open application to send a message and then automatically close it. Keep in mind this is not a solution this is a hacky workaround and should not be used in production, just for the sake of interest, I think it's doable like this.

tatocaster
  • 316
  • 3
  • 10
0

You can do that only using the Accessibility API of Android.

The idea is quite simple, you'll actually make Android perform the click on Whatsapp's send button.

So the flow will be:

  1. Send a regular message (with the intent you're currently using) with a suffix at the end of your message content such as "Sent by MY_APP".

  2. Once the text there, your accessibility service will be notified that the EditText of WhatsApp is filled.

  3. If the suffix is present on the EditText of WhatsApp, Your accessibility service will click on the send button. (this is to avoid performing actions as the user types in naturally a regular message).

Check the link for code click here for more information

Krishna Sony
  • 1,286
  • 13
  • 27