简体   繁体   中英

SMS Permissions

I am building an Android app via android studio. One major feature of the app is sending SMS but I cannot seem to get it to run correctly on different devices due to permission errors.I tried to eliminate this issue by prompting for permissions when the user logs in but this worked on API version 13 and failed on 26,I tried it with build.versioncode.o. but his also failed. What is the correct way to do this and to check permissions each time a SMS is sent?(note sms are sent from many different functions throughout the app.

I also just got this error hen sending message

java.lang.SecurityException: Neither user 10205 nor current process has android.permission.READ_PHONE_STATE.

Request permission on log in

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void getPermissionToReadSMS() {
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (shouldShowRequestPermissionRationale(
                android.Manifest.permission.READ_SMS)) {
            Toast.makeText(this, "Please allow permission!", Toast.LENGTH_SHORT).show();
        }
        requestPermissions(new String[]{android.Manifest.permission.READ_SMS},
                REQUEST_READ_SMS);
    }
}

Method snippet that sends method

    SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(testContact.getNumber(), null, "SENT MESSAGE" + message, sentPending, deliveredPending);

App Manifest

 <!-- grant permission to uses in build sms service -->
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission-group.SMS" />

For checkPermission to send SMS, you will need to add <uses-permission android:name="android.permission.SEND_SMS" /> permission and the below code for >=23 api

 protected void sendSMS() {
     if (ContextCompat.checkSelfPermission(this,
             Manifest.permission.SEND_SMS)
             != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                   Manifest.permission.SEND_SMS)) {
                } else {
                   ActivityCompat.requestPermissions(this,
                      new String[]{Manifest.permission.SEND_SMS},
                      PERMISSION_REQUEST_SEND_SMS);
                }
          }
}
 @Override
   public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
      switch (requestCode) {
         case PERMISSION_REQUEST_SEND_SMS: {
            if (grantResults.length > 0
               && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  SmsManager smsManager = SmsManager.getDefault();
                  smsManager.sendTextMessage(phoneNo, null, message, null, null);
                  Toast.makeText(getApplicationContext(), "SMS sent successfully.", 
                     Toast.LENGTH_LONG).show();
            } else {
               Toast.makeText(getApplicationContext(), 
                  "SMS faild", Toast.LENGTH_LONG).show();
               return;
            }
         }
      }

   } 
 //You should declare this in your project
private static String[] PERMISSIONS_STORAGE = {

            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_SMS,
            Manifest.permission.RECEIVE_SMS,
            Manifest.permission.SEND_SMS,

    };

//and call verifymethod where you want verifyStoragePermissions(Activity_home.this);

//and the verifyStoragePermissions methode is  
 public static void verifyStoragePermissions(Activity activity) {
        int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE);
        int smspermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_SMS);
        int smspermission1 = ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECEIVE_SMS);
        int smssendpermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.SEND_SMS);

        if (readPermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED || smspermission != PackageManager.PERMISSION_GRANTED || smspermission1 != PackageManager.PERMISSION_GRANTED  || smssendpermission != PackageManager.PERMISSION_GRANTED) {userActivityCompat.requestPermissions(activity,PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE );
        }
    }

I'm sending it from a fragment. The solution given by @Android Team above works for me for the first time only. Apparently if the app has required permission it doesnt call onRequestPermissionsResult.

Using the following code (else portion with log msg "Have permission.." in the onlclick handler is required for my app to work). I'm using Android 11 device.

       if (ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        //if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
        if (shouldShowRequestPermissionRationale(
                Manifest.permission.SEND_SMS)) {
        } else {
            //ActivityCompat.requestPermissions(getActivity(),
            requestPermissions(
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
    } else {
        Log.i("smsB", "Have permission... send the sms now");
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, message, null, null);
        Toast.makeText(getContext(), "SMS sent.",
                Toast.LENGTH_LONG).show();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM