-4

I have made an android app on android studio, from that application I want to send a message to a number but via whatsapp. The number is not in my contact list. I have tried the following code found on stackoverflow:

Uri mUri = Uri.parse("smsto:+999999999");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat",true);
startActivity(mIntent);

But it throws an exception "Activity not found".

I have also found one code but it opens the chat screen which I don't want means the user should only get a toast that message is sent and not any other screen.

If anybody has a solution please let me know.

shribhvs
  • 11
  • 1
  • 3
  • 1
    Did you try this - [Sending message through WhatsApp](http://stackoverflow.com/a/15931345/3398732) ? – CodeWalker Sep 02 '16 at 12:20
  • 2
    You cannot send a message without user interaction, that is a terrible idea. See [this thread](http://stackoverflow.com/questions/15462874/sending-message-through-whatsapp) and the [WhatsApp FAQ](https://www.whatsapp.com/faq/de/android/28000012). – Tobias Sep 02 '16 at 12:20
  • see this http://stackoverflow.com/questions/15462874/sending-message-through-whatsapp?noredirect=1&lq=1 – comeback4you Sep 02 '16 at 12:27

3 Answers3

1
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();
    }  

    }

try this,or check this link sharingwithwhatsapp

Pawanpreet
  • 355
  • 1
  • 4
  • 13
0

**

  1. I find this Code stuff from my friend website

**

enter image description here

activity_main.xml

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.code2care.example.whatsappintegrationexample.MainActivity"
    tools:ignore="HardcodedText" >

    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="sendMessae"
    android:layout_marginBottom="21dp"
    android:text="Send to WhatsApp" />

    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:hint="Type your WhatsApp message here ...."
    android:layout_alignParentTop="true"
    android:background="#FFFFFFEF"
    android:padding="10dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="65dp"
    android:layout_height="200dp"
    android:ems="10" />

activity_main.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

    private EditText message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        message = (EditText) findViewById(R.id.message);

    }

    public void sendMessage(View v) {

        String whatsAppMessage = message.getText().toString();

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
        sendIntent.setType("text/plain");

        // Do not forget to add this to open whatsApp App specifically
        sendIntent.setPackage("com.whatsapp");
        startActivity(sendIntent);

    }

}
Sohaib Aslam
  • 1,245
  • 17
  • 27
0

Below mentioned code is used to direct the chat on Whatsapp on any number via android application :

 String url = "https://api.whatsapp.com/send?phone=+9194xxxxxxxx";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
Jagroop
  • 1,777
  • 1
  • 6
  • 20