使用此代码只打开特殊号码的聊天但文本不是共享.我该怎么做?
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(); }