Redmi 手机中的运行时权限

Posted

技术标签:

【中文标题】Redmi 手机中的运行时权限【英文标题】:Runtime Permission in Redmi Phone 【发布时间】:2017-02-12 05:37:00 【问题描述】:

我正在尝试发送具有运行时权限的短信目标版本 24。它在 Redmi Mi 3s 设备中提供 SecurityException,但在其他装有 Marshmallow OS 的设备上运行良好。

final int REQ_CODE = 100;
void requestPermission()
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) 
        CTLogs.printLogs( "Permission is not granted, requesting");
        ActivityCompat.requestPermissions(this, new String[]Manifest.permission.SEND_SMS,Manifest.permission.READ_SMS,Manifest.permission.RECEIVE_SMS, REQ_CODE);
       // button.setEnabled(false);
     else 
        CTLogs.printLogs("Permission has been granted");
        sendSMS();
    


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
    if (requestCode == REQ_CODE) 
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
            CTLogs.printLogs("Permission has been granted");
            sendSMS();
            else 
            CTLogs.printLogs("Permission denied !!!");
        
    

【问题讨论】:

您是否在清单中添加了权限以及您的应用是否为默认消息应用? 是的,我也在清单文件中添加了权限 如果你想要一个简单且有效的代码,你可以从github.com/mptrista/PermissionHelper添加一个类 【参考方案1】:

试试这个:

// The permission required by the application to work properly
    protected static final String[] requiredPermissions;
    private static final int PERMISSION_REQUEST = 0;

    static 
        List<String> perms = new ArrayList<>(Arrays.asList(
                Manifest.permission.RECEIVE_SMS,
                Manifest.permission.READ_SMS,
                Manifest.permission.SEND_SMS
        ));
        requiredPermissions = perms.toArray(new String[perms.size()]);
    

在onCreate()中调用verifyPermissions()方法:

private void verifyPermissions() 
        if (!hasAllPermissions()) 
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    this,
                    requiredPermissions,
                    PERMISSION_REQUEST
            );
        
    

    private boolean hasAllPermissions() 
        // Check if we have all required permissions.
        for (String perm : requiredPermissions) 
            if (ActivityCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) 
                return false;
            
        
        return true;
    

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) 
        switch (requestCode) 
            case PERMISSION_REQUEST: 
                // If request is cancelled, the result arrays are empty.
                if (!hasAllPermissions()) 
                    finish();
                
                return;
            
        
    

【讨论】:

酷..我的回答对你有帮助吗?【参考方案2】:

一旦卸载应用程序然后把它然后安装它。

if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) 


        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]Manifest.permission.SEND_SMS,
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    

【讨论】:

如果可能,请在您的答案中添加更多详细信息:)

以上是关于Redmi 手机中的运行时权限的主要内容,如果未能解决你的问题,请参考以下文章

在 Redmi 设备上启用“在锁定屏幕上显示”权限

权限 - 如何在运行时运行 MIUI 的手机中请求和/或更改它们?

红米RedMi Note 8刷入magisk(root权限)

Android运行时权限中永远不会显示“不再询问”复选框

系统应用的 Android 运行时权限

Android 6.0 运行时权限处理解析