发送短信通过Whatsapp

时间:2017-06-09 06:15:41

标签: android textview send whatsapp

我需要通过whatsapp方法将布局的所有textview作为字符串发送,但如果textview为null,则必须发送空白文本。

这是我的代码:

public void sendButton(View view) {
    // Do something in response to button

    PackageManager pm = getPackageManager();
    try {

        TextView textview3 = (TextView) findViewById(R.id.textAmor3);
        TextView textview2 = (TextView) findViewById(R.id.textAmor2);

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");

        if (textview3 == null) {


        } else {

        }




        String both = textview2 + "-" + textview3;

        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, both );

        startActivity(Intent.createChooser(waIntent, "Share with"));

    } catch (PackageManager.NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
    }
}
}

我该怎么做?

感谢

2 个答案:

答案 0 :(得分:0)

更改此

String both = textview2 + "-" + textview3;

String both = textview2.getText().toString() + "-" + textview3.getText().toString();

答案 1 :(得分:0)

您无法与任何应用分享Textviews。您可以共享写在Textviews上的文本。所以使用textview3.getText().toString()获取文本,然后将其传递给应用程序的意图。

Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");

try {
    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, "textString here" );

    startActivity(Intent.createChooser(waIntent, "Share with"));

} catch (PackageManager.NameNotFoundException e) {
    Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
            .show();
}