Frage

How can I transfer a text from EditText input to compose message. Say for example:

Enter phone number: 2366 Enter message: MUSIC ON

and it will generate automatically in Compose Message

To: 2366

Message: MUSIC ON

War es hilfreich?

Lösung

Try your own:

You have 2 EditTexts as,

EditText ed_to;
EditText ed_message;

Before using following code, initialize EditTexts using findViewById()

String to=ed_to.getText.toString();
String message=ed_message.getText.toString();
 SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(to, null, message, null, null); 

Update: While using above code, you have to set permission

<uses-permission android:name="android.permission.SEND_SMS"/>

This will not show you the compose message window, instead, directly send sms.

If you don't want to set permission and want to see compose message, use following:

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

Andere Tipps

see following code it may help you

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

also see this link Sending Message

You do it this its simple one...

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

I think this is what you want exactly

 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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top