Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

android - How to give run time permission for marshmallow for read SMS from inbox in eclipse

I have applied multiple code but it's show me checkSelfPermission() and shouldShowRequestPermissionRationale methods not exits, please help me..

I used brodcast receiver class for reading SMS it's working fine for jellybean to lolipop but not for marshmallow and lower versions than jellybean like icecream sandwitch, how can i make application to compatible for all android versions,

please guide me step by step because i'm new for this and please give me example also.

hear is my code for read sms permission..

public void getpermission(){


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

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

            // REQUEST_CODE_ASK_SINGLE_PERMISSION is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can do it by extending this given class in your Activity,

public class BasePermissionAppCompatActivity extends AppCompatActivity {

private final static String APP_NAME = "APP_NAME";
private final static int REQUEST_READ_SMS_PERMISSION = 3004;
public final static String READ_SMS_PERMISSION_NOT_GRANTED = "Please allow " + APP_NAME + " to access your SMS from setting";

RequestPermissionAction onPermissionCallBack;

private boolean checkReadSMSPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

public void getReadSMSPermission(RequestPermissionAction onPermissionCallBack) {
    this.onPermissionCallBack = onPermissionCallBack;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!checkReadSMSPermission()) {
            requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_READ_SMS_PERMISSION);
            return;
        }
    }
    if (onPermissionCallBack != null)
        onPermissionCallBack.permissionGranted();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        if (REQUEST_READ_SMS_PERMISSION == requestCode) {
            // TODO Request Granted for READ_SMS.
            System.out.println("REQUEST_READ_SMS_PERMISSION Permission Granted");
        }
        if (onPermissionCallBack != null)
            onPermissionCallBack.permissionGranted();

    } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
        if (REQUEST_READ_SMS_PERMISSION == requestCode) {
            // TODO REQUEST_READ_SMS_PERMISSION Permission is not Granted.
            // TODO Request Not Granted.


            // This code is for get permission from setting.
            //final Intent i = new Intent();
            //i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            //i.addCategory(Intent.CATEGORY_DEFAULT);
            //i.setData(Uri.parse("package:" + getPackageName()));
            //i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            //i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            //startActivity(i);
        }
        if (onPermissionCallBack != null)
            onPermissionCallBack.permissionDenied();
    }
}

public interface RequestPermissionAction {
    void permissionDenied();

    void permissionGranted();
}

}

like this

public class ActivityMain extends BasePermissionAppCompatActivity

now in your ActivityMain you can get permission by this code block

getReadSMSPermission(new RequestPermissionAction() {
@Override
public void permissionDenied() {
    // Call Back, when permission is Denied
    // TODO, task after permission is not greante
}

@Override
public void permissionGranted() {
    // Call Back, when permission is Granted
    // TODO, task after permission is greante
}
});

And One more thing, you need to add a permission in your manifest

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...