我正试图在通知来自服务器时在应用图标上显示徽章。
我得到了这个例外:
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.sec.android.provider.badge.BadgeProvider from ProcessRecord{ab1124 10588:com.rehq.app/u0a175} (pid=10588, uid=10175) requires com.sec.android.provider.badge.permission.READ or com.sec.android.provider.badge.permission.WRITE
答案 0 :(得分:1)
从Android 6.0开始,您需要在运行时请求权限。
以下示例适用于WRITE权限(我猜你想在应用图标上添加徽章,通常你不需要READ权限 - 如果你需要它,你可以申请它就像WRITE权限一样)
首先在Manifest中添加权限:
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>
然后你可以检查它们是否在运行时被授予如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
//permissions not granted -> request them
requestPermissions(new String[] {Manifest.permission.WRITE_SETTINGS}, YOUR_REQUEST_CODE);
} else {
//permissions are granted - do your stuff here :)
}
结果将在onRequestPermissionResult
中提供:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == YOUR_REQUEST_CODE) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED) {
//permissions granted -> do your stuff ;-)
}
//Permission not granted -> react to it!
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
您可以在official docs
中详细了解相关信息