【问题标题】:Getting Text String from EditText and transfer it to compose message [closed]从 EditText 获取文本字符串并将其传输到撰写消息 [关闭]
【发布时间】:2013-08-05 03:49:03
【问题描述】:

如何从 EditText 输入中传输文本以撰写消息。比如说:

输入电话号码: 2366 输入消息: 音乐开启

它会在 Compose Message 中自动生成

收件人:2366

消息:音乐开启

【问题讨论】:

  • “撰写邮件”是什么意思?

标签: android android-2.3-gingerbread android-4.3-jelly-bean


【解决方案1】:

试试你自己的:

您有 2 个 EditText,

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

【讨论】:

  • 谢谢。顺便说一句,如果我选择第一种方式,我不想看到写消息,而是直接发送,我怎么知道消息是否已发送?或者我怎样才能放置一条表明它已发送的消息?我应该把它放在:sms.sendTextMessage(to, null, message, null, null);
  • 看大卫的回答。如果您真的想监控,最好使用那些 PendingIntents sms.sendTextMessage(to, null, message, sentPI, deliveredPI)。否则,就在它之后敬酒。
【解决方案2】:

看下面的代码可能对你有帮助

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);        
    }

另见此链接 Sending Message

【讨论】:

  • 获取字符串到 的部分在哪里?
  • 您可以通过 String Text = Your_EditText_ID.getText().toString(); 获取您的文本;
  • 是的,先生,但我不需要发送消息,我只想将文本从文本框 (EditText) 传输到消息的撰写消息部分
  • 试试这个 Intent smsIntent = new Intent(Intent.ACTION_VIEW); smsIntent.setType("vnd.android-dir/mms-sms"); smsIntent.putExtra("地址", "12125551212"); smsIntent.putExtra("sms_body","消息正文"); startActivity(smsIntent);
【解决方案3】:

你这样做很简单......

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

【讨论】:

    【解决方案4】:

    我想这正是你想要的

     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();
            }
        });
    

    【讨论】:

      【解决方案5】:

      我认为你必须初始化一个变量,比如说字符串电话,并使用 findViewById("phoneEditText"); 获取电话的对象 editTex;那么字符串 phone 将具有您想要的值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-18
        相关资源
        最近更新 更多