0

Sending an SMS (not just creating the text and requiring the user to hit send) can't be that difficult, and yet I've spent the past eleven hours reading every stackoverflow entry and failing nonetheless. I'm now wondering if maybe the method has changed recently since none of the examples seem to work. If someone could help me with this, I'd be grateful. This is my first post, so my apologies if I'm not following the conventions of the community.

Here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="joe.sms">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And here is my MainActivity:

package.joe.sms;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("+1716255xxxx", null, "Test message", null, null);
    }
}

Note: I use my full phone number in the actual code.

And here is the result:

error message screen shot

Thanks, in advance, for your assistance.

Joseph Little
  • 35
  • 1
  • 5
  • 2
    Examine the Java stack trace in LogCat: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Also note that if your `targetSdkVersion` is 23 or higher, and you are testing on Android 6.0+, [you need to deal with runtime permissions for `SEND_SMS`](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it). – CommonsWare Dec 06 '17 at 17:25
  • 1
    Android Studio is not able to send SMS – Henry Dec 06 '17 at 17:25
  • Post the Stack Trace from Logcat – anemo Dec 06 '17 at 17:47
  • Thanks very much, everyone. Your combined comments enabled me to handle the runtime permissions as required. – Joseph Little Dec 06 '17 at 18:25

1 Answers1

2

Implement the code like this,

  private static final int SEND_SMS_CODE = 23;

Checking permission is given, if permission is granted send SMS else asking for the permission.

private void permissionCheck(){
    if (isSendSmsAllowed()) {
          sendSms();
          return;
      }

    requestSmsSendPermission();}

Function to send SMS.

private void sendSms(){
    SmsManager sms = SmsManager.getDefault();
              ArrayList<String> parts = sms.divideMessage("MESSAGE");
              sms.sendMultipartTextMessage(90******, null, parts, null,
                null);}

Requesting permission if not granted.

//Requesting permission
  private void requestSmsSendPermission() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)) {
      //If the user has denied the permission previously your code will come to this block
      //Here you can explain why you need this permission
      //Explain here why you need this permission
    }

    //And finally ask for the permission
    ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.SEND_SMS },
      SEND_SMS_CODE);
  }

Hope it may help you.

Sachin Varma
  • 2,175
  • 4
  • 28
  • 39
  • Thanks very much, Sachin. Your approach, combined with one of the links offered above, solved the problem! It was indeed getting hung up on the Runtime SMS permission issue. – Joseph Little Dec 06 '17 at 18:24
  • I was able to mark your comment as accepted, but when I up-voted your comment, a message informed me that it will not be made public because I have too few "reputation points" or something like that. Thanks again. I really appreciate your help. – Joseph Little Dec 06 '17 at 18:36
  • No issues, happy coding :) – Sachin Varma Dec 06 '17 at 18:46