0

Hi guys I am new to programming. The code below is able predefined SMS to predefined number by pressing button but it will bring to the message composer screen and requires me to click send. How do I send the SMS directly without going to the message composer. I see few people are asking this too and one of the solution is using SMSmanger but i do not know how to use SMSmanger code into my code.

public class SendSMSActivity extends Activity {

Button buttonSend;    
Button buttonSend2;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);     

    buttonSend = (Button) findViewById(R.id.buttonSend);     
    buttonSend2 = (Button) findViewById(R.id.buttonSend2);



    OnClickListener listener = new OnClickListener() {        

        @Override
        public void onClick(View v) {   

            switch (v.getId()) {    

            case R.id.buttonSend:     
                 Intent sendIntent = new Intent(Intent.ACTION_VIEW);  
                 sendIntent.putExtra("sms_body", "#abc"); 
                 sendIntent.putExtra("address", "9900990");   
                 sendIntent.setType("vnd.android-dir/mms-sms");
                 startActivity(sendIntent);       
                 break;

            case R.id.buttonSend2: 
                Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
                 sendIntent1.putExtra("sms_body", "#def"); 
                 sendIntent1.putExtra("address", "9900990");
                 sendIntent1.setType("vnd.android-dir/mms-sms");
                 startActivity(sendIntent1);
                 break;

            }

        }
    };

buttonSend.setOnClickListener(listener);       
buttonSend2.setOnClickListener(listener);
}
)

Thank you

Neha Agarwal
  • 622
  • 7
  • 24

2 Answers2

0

try following:

SmsManager sms = SmsManager.getDefault();  
sms.sendTextMessage(phoneNumber, null, message, null, null);

Note : Its will require permission android.permission.SEND_SMS

Yogesh Lakhotia
  • 888
  • 1
  • 13
  • 26
0

MainActivity

    EditText etPhone;
    EditText etMessage;

    Button button;


    public String[] mPermission = {SEND_SMS, READ_SMS};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etPhone = findViewById(R.id.main_et_phone);
        etPhone = findViewById(R.id.main_et_message);
        button = findViewById(R.id.main_btn_send);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SendSMS();
            }
        });
    }

    private void SendSMS() {

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("SMS_SEND"), 0);
        PendingIntent pendingIntentDel = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("SMS_DELIVERED"), 0);

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(etPhone.getText().toString(), null, etMessage.getText().toString(), pendingIntent, pendingIntentDel);
    }

    private void SMSPermission() {

        ActivityCompat.requestPermissions(MainActivity.this, mPermission, 1);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SMSPermission();
    }
Reza
  • 1
  • 3