如何从 EditText 输入中传输文本以撰写消息。比如说:
输入电话号码:2366 输入信息:MUSIC ON
它将在撰写消息中自动生成
至:2366
消息:音乐开启
如何从 EditText 输入中传输文本以撰写消息。比如说:
输入电话号码:2366 输入信息:MUSIC ON
它将在撰写消息中自动生成
至:2366
消息:音乐开启
请参阅以下代码,它可能会对您有所帮助
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
另请参阅此链接 发送消息
试试你自己的:
您有 2 个 EditTexts,
EditText ed_to;
EditText ed_message;
在使用以下代码之前,使用 findViewById() 初始化 EditTexts
String to=ed_to.getText.toString();
String message=ed_message.getText.toString();
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(to, null, message, null, null);
更新:使用上述代码时,您必须设置权限
<uses-permission android:name="android.permission.SEND_SMS"/>
这不会向您显示撰写消息窗口,而是直接发送短信。
如果您不想设置权限并想查看撰写消息,请使用以下命令:
String to=ed_to.getText().toString();
String msg=ed_message.getText().toString();
Intent smsIntent= new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", to);
smsIntent.putExtra("sms_body",msg);
startActivity(smsIntent);
你这样做很简单...
Intent intentSendMessage= new Intent(Intent.ACTION_VIEW);
intentSendMessage.setData(Uri.parse("sms:"));
smsIntent.putExtra("address", "12125551212");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(intentSendMessage);
我认为这正是您想要的
final Button buttonLaunchSMS= (Button)findViewById(R.id.ButtonLaunchSMSMessage);
buttonLaunchSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String outCipherText= editTextSMSCipherText.getText().toString();
String phoneNumber= editTextPhoneNumber.getText().toString();
// pre-conditions
if (outCipherText.length() < 1){
editTextSMSCipherText.setError("Cipher Text is Empty");
editTextSMSCipherText.requestFocus();
return;
}
if (outCipherText.length()>MAX_SMS_CHAR){
editTextSMSCipherText.setError("Error. Message Is Too Large.");
editTextSMSCipherText.requestFocus();
return;
}
String uri= "smsto:"+phoneNumber;
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
intent.putExtra("sms_body", outCipherText);
intent.putExtra("compose_mode", true);
startActivity(intent);
finish();
}
});
I think you have to inicializate a variable, lets say String phone, and get the object editTex of phone by using the findViewById("phoneEditText"); then the string phone would have the value you want