11

I am trying to automatically send SMS message to a certain number when the user presses a button on the screen.

This is my code:

Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("sms:xxxxxxxxxxx")); 
smsIntent.putExtra("sms_body", "Hello");
startActivity(smsIntent);

xxxxxxx = phone number

I have the following permissions:

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

When I press the button it takes me to another screen where I can edit my text and press Send. I just want it to do this process automatically without taking me to another screen. As I've already defined my message, I just want to send it to a particular number.

And also I am not sure if I put the corrent phone number in the second line of code. Do I have to put my country code in there first or can I just put my mobile phone number and it will work?

Thank you

Alex
  • 876
  • 6
  • 17
  • 38

5 Answers5

22

Try this code:

 String messageToSend = "this is a message";
 String number = "2121234567";

 SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);

With regards to the number, you need to enter the number as if you were calling it from the phone or sending an sms message in the normal manner.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • 1
    @chris I am using your code but unable to receive sms on my no. whichever i am putting as Number – Shani Goriwal Mar 19 '14 at 09:06
  • 2
    How can we detect the failure(due to cases like SIM unavailable or network issue) of sending SMS in this code? – venkat Dec 10 '14 at 13:27
  • After a new Google policy change make sure that your app is elligible to use the SMS permission to use this command when released to the play store. https://play.google.com/about/privacy-security-deception/permissions/ – Andrew Heschl Mar 30 '19 at 15:58
3

You can use the build in Intent also:

 buttonSendSms_intent.setOnClickListener(new Button.OnClickListener(){

       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub

        String smsNumber = edittextSmsNumber.getText().toString();
        String smsText = edittextSmsText.getText().toString();

        Uri uri = Uri.parse("smsto:" + smsNumber);
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", smsText);  
        startActivity(intent);
       }});
K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • 1
    The problem with this is that if there are multiple activities capable of handling the `Intent.ACTION_SENDTO` action, a picker will be displayed to allow the user to select an app to send the sms from (therefore requires human intervention in that case). OP wanted to do it automatically without human intervention I am assuming. –  Mar 12 '17 at 06:28
2

Try this

private static final String SMS_SENT_INTENT_FILTER = "com.yourapp.sms_send";
private static final String SMS_DELIVERED_INTENT_FILTER = "com.yourapp.sms_delivered";

String message = "hey, this is my message";

String phnNo = " " //preferable use complete international number

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SMS_SENT_INTENT_FILTER), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SMS_DELIVERED_INTENT_FILTER), 0);

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phnNo, null, message, sentPI, deliveredPI);
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
2

the easy way is to use SmsManager.Telephony.

eVoxmusic
  • 673
  • 5
  • 18
1

First set following permissions:

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

then create following activity:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <EditText
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="41dp"
            android:layout_marginLeft="41dp"
            android:layout_marginRight="41dp"
            android:layout_marginStart="41dp"
            android:layout_marginTop="43dp"
            android:ems="10"
            android:hint="Enter Phone no"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="37dp"
            android:layout_marginLeft="37dp"
            android:layout_marginRight="37dp"
            android:layout_marginStart="37dp"
            android:layout_marginTop="28dp"
            android:ems="10"
            android:hint="Enter text"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="183dp"
            android:text="SEND"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </android.support.constraint.ConstraintLayout> 

Now go to your MainActivity.java File and add following code:

public class MainActivity extends AppCompatActivity {

    EditText e1, e2;
    Button b1;
    private final static int SEND_SMS_PERMISSION_REQ=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=findViewById(R.id.editText);
        e2=findViewById(R.id.editText2);
        b1=findViewById(R.id.button);
        b1.setEnabled(false);


        if(checkPermission(Manifest.permission.SEND_SMS))
        {
            b1.setEnabled(true);
        }
        else
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQ);
        }
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1=e1.getText().toString();
                String s2=e2.getText().toString();
                if(!TextUtils.isEmpty(s1)&&!TextUtils.isEmpty(s2))
                {

                    if(checkPermission(Manifest.permission.SEND_SMS))
                    {
                        SmsManager smsManager=SmsManager.getDefault();
                         smsManager.sendTextMessage(s1,null,s2,null,null);
                    }
                    else {
                        Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private boolean checkPermission(String sendSms) {

        int checkpermission= ContextCompat.checkSelfPermission(this,sendSms);
        return checkpermission== PackageManager.PERMISSION_GRANTED;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode)
        {
            case SEND_SMS_PERMISSION_REQ:
                if(grantResults.length>0 &&(grantResults[0]==PackageManager.PERMISSION_GRANTED))
                {
                    b1.setEnabled(true);
                }
                break;
        }
    }
}

Done it successfully.

mehdi
  • 340
  • 4
  • 17