0

I am trying to send multiple data from database as an sms in the registered number. But I keep getting this as an error:

Sending SMS message: uid 10231 does not have android.permission.SEND_SMS

I already granted permission in my manifest to send sms.

Login Class


    public void permission()
    {
        String[] permission={Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_SMS,
                Manifest.permission.RECEIVE_SMS,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.SEND_SMS};

        if(ContextCompat.checkSelfPermission(Login.this,
                permission[0])== PackageManager.PERMISSION_GRANTED);

        else
        {
            ActivityCompat.requestPermissions(Login.this,permission,1);

        }
    }
           @Override
            public void onResponse(Call<ResObj> call, Response<ResObj> response) {

                ResObj resObj = response.body();

                if(response.isSuccessful()) {

                    String phoneNum = et_mobile.getText().toString();

                    SmsManager smsManager = SmsManager.getDefault();
                    String spamMessage = "Srno: " + resObj.getSrNo() +
                            "Date: "  + resObj.getDate() +
                            "In: " + resObj.getTimeIn() +
                            "Out: " + resObj.getTimeOut() +
                            "JO#: " + resObj.getJoNo() +
                            "CoPer: " + resObj.getContactPerson() +
                            "BR: " + resObj.getDesignation() +
                            "CNum: " + resObj.getContactNo();
                    ArrayList<String> sms;
                    sms = smsManager.divideMessage(spamMessage);
                    smsManager.sendMultipartTextMessage(phoneNum, null,sms,null, null);

                    //smsManager.sendTextMessage(phoneNum, null, spamMessage, null, null);

                    Intent intent = new Intent(Login.this, ListActivity.class);
                        intent.putExtra("mobile", mobile);
                        startActivity(intent);



                    } else{
                        Toast.makeText(Login.this, "Phone Number is incorrect!", Toast.LENGTH_SHORT).show();

                    }
                }

Android Manifest

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <uses-permission android:name="android.permission.SEND_SMS"/>
Angel
  • 175
  • 1
  • 3
  • 10
  • 2
    You need to ask the user to forcefully allow the permission only asking that in manifest wouldn't allow that permission. – Abhishek Nov 27 '19 at 06:51
  • As @Abhishek mentioned above you have to request for app permission in your code runtime. refer https://developer.android.com/training/permissions/requesting.html for more information – Vinay Jayaram Nov 27 '19 at 06:53
  • you have to run time permission for that only manifest permission code does't work. – Milan Tejani Nov 27 '19 at 06:58
  • I updated my post. Thanks! – Angel Nov 27 '19 at 07:12
  • That is not a [mcve], so we can't really tell you where you're going wrong. You simply do not have the `SEND_SMS` permission granted when you try to send. If you want to send SMS, you need to specifically check that you have the `SEND_SMS` permission _before_ the `SmsManager` send. You're not checking for that. The only check I see in that code is for `permission[0]`, which is `READ_EXTERNAL_STORAGE`. – Mike M. Nov 27 '19 at 07:21
  • @MikeM.Oops. My bad. i changed permission[0] to permission[5]. Now it gives me this error: `E/SmsManager: Unable to launch Settings application` – Angel Nov 27 '19 at 07:25
  • Well, that's a different issue. However, it looks like it's trying to launch a SIM selection page, and failing. Are you testing this on a physical device? If so, is it multi-SIM? – Mike M. Nov 27 '19 at 07:33
  • @MikeM.Yes. I am testing on a physical device and it is dual sim but there is only one sim inside. – Angel Nov 27 '19 at 07:38
  • Well, it seems like you're going to have to properly handle multi-SIM to get it working on that particular device. Basically, that means you'll need to use `SmsManager.getSmsManagerForSubscriptionId()` instead of `SmsManager.getDefault()`. I've never worked with multi-SIM before, but there's a generic example of using that method, and getting the subscription ID, in [this answer](https://stackoverflow.com/a/51380282). – Mike M. Nov 27 '19 at 07:44

0 Answers0