如何在特定号码的应用程序中共享文本

时间:2016-02-06 06:50:58

标签: android whatsapp

使用此代码仅打开特殊号码的聊天但文字不是共享。我该怎么做?

public class MainActivity extends AppCompatActivity {
Button Wa;
String id = "+919000000000";
EditText txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt = (EditText)findViewById(R.id.editText);
    Wa = (Button)findViewById(R.id.btn_whatsapp);
    Wa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("smsto:" + id);
            Intent waIntent = new Intent(Intent.ACTION_SENDTO,uri);

            String text = "testing message";
            waIntent.setPackage("com.whatsapp");
            if (waIntent != null) {
                waIntent.putExtra(Intent.EXTRA_TEXT, text);
                startActivity(Intent.createChooser(waIntent, text));
            } else {
                Toast.makeText(getApplicationContext(), "WhatsApp not found", Toast.LENGTH_SHORT)
                        .show();
            }

         }
    });

}

4 个答案:

答案 0 :(得分:1)

由于您尝试将其作为"smsto:"来实现,因此类型"text/plain"会对您有所帮助。如果它没有帮助,请尝试额外为"sms_body"

Uri uri = Uri.parse("smsto:" + id);
Intent waIntent = new Intent(Intent.ACTION_SENDTO,uri);
String text = "testing message";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
    waIntent.setType("text/plain");
    //waIntent.putExtra(Intent.EXTRA_TEXT, text);
    waIntent.putExtra("sms_body", text); 
    startActivity(Intent.createChooser(waIntent, text));
} else {
    Toast.makeText(getApplicationContext(), "WhatsApp not found", Toast.LENGTH_SHORT)
            .show();
}

答案 1 :(得分:0)

请完成此stackoverflow链接

  1. Sending message through WhatsApp
  2. Send text to specific contact
  3.   

    似乎 WhatsApp 仍然没有支持这个胎儿。   您只能使用以下代码

    打开特定号码的聊天记录
    try {
                Uri mUri = Uri.parse("smsto:+98xxxxxxxx");
                Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
                mIntent.setPackage("com.whatsapp");
    
    //            mIntent.putExtra("sms_body", "My body");
                mIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    
                startActivity(mIntent);
            } catch (Exception e) {
              // alert WhatsApp in not installed
            }
    

答案 2 :(得分:0)

尝试这样的事情:

public class MainActivity extends AppCompatActivity {
    Button Wa;
    String id = "+919000000000";
    EditText txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (EditText)findViewById(R.id.editText);
        Wa = (Button)findViewById(R.id.btn_whatsapp);
        Wa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PackageManager pm=getPackageManager();
                try {
                    Uri uri = Uri.parse("smsto:" + number);
                    Intent i = new Intent(Intent.ACTION_SENDTO, uri);
                    waIntent.setType("text/plain");
                    String text = "testing message";

                    PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
                    waIntent.setPackage("com.whatsapp");
                    waIntent.putExtra(Intent.EXTRA_TEXT, text);
                    startActivity(Intent.createChooser(waIntent, text));
                }   
                catch (NameNotFoundException e) {
                    Toast.makeText(this, "WhatsApp not found", Toast.LENGTH_SHORT).show();
                }  
             }
        });
    }
}

答案 3 :(得分:0)

来自Here

的回答

早些时候,这是不可能的,但自2015年5月更新以来。结帐:

try{
    PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    String sendString = "some random string";
    sendIntent.setPackage("com.whatsapp");
    sendIntent.putExtra(Intent.EXTRA_TEXT, sendString);
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    sendIntent.setType("image/*");
    startActivity(sendIntent);
} catch (Exception e){
    // some code
}

这里的PackageInfo行只是检查是否安装了WhatsApp。如果没有,它会抛出异常。如果你想做一个普通的共享(和setPackage),你可以忽略它。

另外。重要的是,您要共享的媒体必须在本地存储上公开。

<强>更新

发送给特定联系人

Uri uri = Uri.parse("smsto:" + "<CONTACT_NUMBER>");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);

现在允许行动发送至