如何在MIUI 8+中获取读取“服务SMS”的权限(以编程方式)

时间:2017-08-18 11:02:10

标签: android permissions sms

如何检查阅读服务我的MI手机应用程序的短信权限已关闭/开启。我正在构建一个应用程序,我需要从手机上阅读短信。现在对于MI手机,一些短信转到通知消息并阅读它们我需要服务短信权限。下面是它的截图。

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您在短信读取代码

之前拨打requestSmsPermission(),则需要获得所有必要的许可
private void requestSmsPermission() {
    String permission = Manifest.permission.READ_SMS;
    int grant = ContextCompat.checkSelfPermission(this, permission);
    if (grant != PackageManager.PERMISSION_GRANTED) {
        String[] permission_list = new String[1];
        permission_list[0] = permission;
        ActivityCompat.requestPermissions(this, permission_list, 1);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 1) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {                                      
            Toast.makeText(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
        }
    }

}