我如何在不使用任何按钮的情况下发送消息,当用户在 3 秒消息自动发送到所需号码后完成消息时,假设在此处输入接收者号码是我的代码。
public void onActivityResult(int requestCode, int resultCode, Intent data){
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String topResult = matches.get(0);
EditText editText = (EditText)findViewById(R.id.editTextmessage);
EditText editText1 = (EditText)findViewById(R.id.toNumber);
String phoneNo = null;
String sms = null;
switch(requestCode){
case check1:
editText1.setText(topResult);
phoneNo=editText1.getText().toString();
break;
case check:
editText.setText(topResult);
sms=editText.getText().toString();
break;
}
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "Your Message Sent Successfully",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Sending fail, Please try again!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}