Android SMS manager getting error

957 views Asked by At

I am trying to send a simple sms. The thing is it is working on the samples that i downloaded from tutorials. But when I am trying to replicate the same code, I am getting error. Below is the code when I tried to debug.

D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
W/System.err: java.lang.SecurityException: Sending SMS message: uid 10333 does not have android.permission.SEND_SMS.
W/System.err:     at android.os.Parcel.readException(Parcel.java:1620)
W/System.err:     at android.os.Parcel.readException(Parcel.java:1573)
W/System.err:     at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:1577)
W/System.err:     at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:380)
W/System.err:     at android.telephony.SmsManager.sendTextMessage(SmsManager.java:333)
W/System.err:     at com.creations.oreo.valletcall.MainActivity$1.onClick(MainActivity.java:40)
W/System.err:     at android.view.View.performClick(View.java:5697)
W/System.err:     at android.widget.TextView.performClick(TextView.java:10814)
W/System.err:     at android.view.View$PerformClick.run(View.java:22526)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:158)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7229)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Disconnected from the target VM, address: 'localhost:8616', transport: 'socket'

I am trying to debug by connecting an actual phone and the permission error probably the obvious one from the above is actually already defined in the Android Manifest file.

My MainActivity as below

public class MainActivity extends AppCompatActivity {

    Button buttonSend;

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




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

        buttonSend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



                try {

                   /* Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                    sendIntent.putExtra("sms_body", "default content");
                    sendIntent.setType("vnd.android-dir/mms-sms");
                    startActivity(sendIntent);*/

                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage("91195525", null, "Test", null, null);
                    Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();


                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),
                            "SMS faild, please try again later SMS!",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

            }
        });


    }
}

My Android Manifest file

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

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



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

The tutorial for example I am following is this

http://www.mkyong.com/android/how-to-send-sms-message-in-android/

2

There are 2 answers

4
Pier Giorgio Misley On

From the official doc

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.SEND_SMS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.SEND_SMS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

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

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

And you can read the result with

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_SEND_SMS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

This way you are asking for permissions runtime, now you gotta implmeent those methods to make them work in your app.

Hope this helps.


Note: another way for sending sms is through intents

0
HARSH PATEL On

You just need to add +91 or any other country code at phonenumber

smsManager.sendTextMessage("+9191195525", null, "Test", null, null);