无法从小米手机以编程方式读取消息

时间:2017-12-08 12:47:27

标签: android sms android-permissions telephonymanager xiaomi

我正在使用EasyPermission从用户处获取permissions

@AfterPermissionGranted(PERMISSIONS_REQUEST_READ_SMS)
private void readSMS() {
    String[] perms = {Manifest.permission.READ_SMS};
    if (EasyPermissions.hasPermissions(this, Manifest.permission.READ_SMS)) {
        initSMS();
    } else {
        // Do not have permissions, request them now
        EasyPermissions.requestPermissions(this, getString(R.string.send_sms_rationale),
                PERMISSIONS_REQUEST_READ_SMS, Manifest.permission.READ_SMS);
    }
}

在我的initSMS()中,我尝试过阅读Android设备上的邮件。它适用于除Xiaomi设备之外的所有设备。我使用TelephonyProvider类来阅读消息。

private void initSMS() {
    TelephonyProvider provider = new TelephonyProvider(this);
    List<Sms> sms = provider.getSms(TelephonyProvider.Filter.INBOX).getList();
    for(int i = 0; i <= sms.size() - 1; i++) {
        Log.d("Address",sms.get(i).address);
        Log.d("Sms",sms.get(i).body);
        Log.d("ReceivedDate",""+sms.get(i).receivedDate));
        Log.d("State",""+sms.get(i).status);
        Log.d("person",""+sms.get(i).person);
    }
}

他们以任何方式阅读Xiaomi设备上的所有邮件吗?

1 个答案:

答案 0 :(得分:5)

我希望这对你有用。首先你必须调用函数isMIUI()来检查miui mobile然后调用上面的函数。此功能将重定向到设置以获取服务短信权限。

private void onDisplayPopupPermission() {

        try {
            // MIUI 8
            Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            localIntent.putExtra("extra_pkgname", getPackageName());
            localIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            localIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(localIntent);


        } catch (Exception e) {
            try {
                // MIUI 5/6/7
                Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                localIntent.putExtra("extra_pkgname", getPackageName());
                localIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                localIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(localIntent);


            } catch (Exception e1) {
                // Otherwise jump to application details
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            }
        }

    }