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

前端之家收集整理的这篇文章主要介绍了android – 如何在特定号码的应用程序中共享文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用此代码只打开特殊号码的聊天但文本不是共享.我该怎么做?
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();
            }

         }
    });

}

解决方法

由于您尝试将其实现为“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(),Toast.LENGTH_SHORT)
            .show();
}
原文链接:https://www.f2er.com/android/318474.html

猜你在找的Android相关文章