当我使用com.sec.android.provider.badge.BadgeProvider时在android 6.0中获取异常

时间:2017-05-23 09:53:29

标签: android

 Caused by: java.lang.SecurityException: Permission Denial: opening provider com.sec.android.provider.badge.BadgeProvider from ProcessRecord{8beeb65 18868:com.rehq.app/u0a175} (pid=18868, uid=10175) requires com.sec.android.provider.badge.permission.READ or com.sec.android.provider.badge.permission.WRITE

任何人都有任何想法来解决此异常?

在Android 6.0中生成此异常。

我不知道如何在android 6.0上提供com.sec.android.provider.badge.permission.READ或com.sec.android.provider.badge.permission.WRITE权限

1 个答案:

答案 0 :(得分:1)

您收到错误是因为您不允许权限,因为启动Android 6.0您必须明确要求权限

首先在清单中声明权限为

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

然后在您的活动中请求许可如下

boolean permission;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        permission = Settings.System.canWrite(context);
    } else {
        permission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
    }
    if (permission) {
        //do your code
    }  else {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            context.startActivityForResult(intent, MainActivity.CODE_WRITE_SETTINGS_PERMISSION);
        } else {
            ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_SETTINGS}, MainActivity.CODE_WRITE_SETTINGS_PERMISSION);
        }
    }

希望这有帮助