-1

I am attempting to create a button to send an SMS to a specific number. However, even though Android Studio has no issue with it, the app crashes on both the emulator and a physical device when the button is pressed.

The error does not make sense, given that the AndroidManifest.xml gives the correct permissions.

Any ideas? Thanks in advance.

MainActivity.java

package saluta.salutapanicandreporter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button panicButton = (Button) findViewById(R.id.panicButton);

        panicButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage("111-111-111", null, "RED ALERT. Reply SAFE when safe", null, null);
            }
        });
    }
}

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_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>

        <receiver android:name=".IncomingSms">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

Error

08-10 17:52:55.723 2436-2436/saluta.salutapanicandreporter E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: saluta.salutapanicandreporter, PID: 2436
                                                                             java.lang.SecurityException: Sending SMS message: uid 10079 does not have android.permission.SEND_SMS.
                                                                                 at android.os.Parcel.readException(Parcel.java:1683)
                                                                                 at android.os.Parcel.readException(Parcel.java:1636)
                                                                                 at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:768)
                                                                                 at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310)
                                                                                 at android.telephony.SmsManager.sendTextMessage(SmsManager.java:293)
                                                                                 at saluta.salutapanicandreporter.MainActivity.sendSMS(MainActivity.java:16)
                                                                                 at saluta.salutapanicandreporter.MainActivity.access$000(MainActivity.java:11)
                                                                                 at saluta.salutapanicandreporter.MainActivity$1.onClick(MainActivity.java:31)
                                                                                 at android.view.View.performClick(View.java:5610)
                                                                                 at android.view.View$PerformClick.run(View.java:22265)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Myl0g
  • 11
  • 5

3 Answers3

1

Also check console for exceptions. Your problem in this case seems to be permissions missing in your manifest file. Add this in your manifest.xml

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

Also check this thread it might SMS permissions.

I hope it helps.

César Ferreira
  • 681
  • 1
  • 5
  • 12
0

are you testing on API >= 23 ? there are new permissions flow called "at runtime" and you should use this

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);

checking for permission already granted by

boolean permissionGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED;

more about in docs

also, I'm not shure, but since KitKat (?) there may be only one app on device which can send SMS (still multiple can read). you have a picker in OS Settings like for browsers. When you have mutliple sending-SMS-apps they are checking (should...) that they are marked as default and able to sending SMS. check this clue

snachmsm
  • 17,866
  • 3
  • 32
  • 74
0

You might be testing Your app on Android M (6) or above?

If yes, then You need run time permissions.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
    // For device above MarshMallow
    boolean permission = getSMSPermission();
    if(permission) {
        // If permission Already Granted
        // Send You SMS here
    }
}
else{
// Send Your SMS. You don't need Run time permission
}


public boolean getWritePermission(){
    boolean hasPermission = (ContextCompat.checkSelfPermission(this, 
    Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED);
    if (!hasPermission) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 10);
    }
    return hasPermission;
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
    {
        case 10: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                // Permission is Granted 
                // Send Your SMS here
            }
         }
    }
}