通知声音未播放。拒绝权限

时间:2018-11-11 09:19:55

标签: android notifications

自定义通知声音不起作用。来自内部存储的声音,文件所有者而不是我的应用程序。

代码:

    Uri sound = Uri.parse(account.notifySound); 
    // "account.notifySound" contains path to audio file, obtained from ringtone picker 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_new_message)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(sound)
            .setContentIntent(pendingIntent);

    if (account.notifyLedColor > 0) notificationBuilder.setLights(MainActivity.ledColorsList[account.notifyLedColor - 1], 100, 50);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(tag, 0, notificationBuilder.build());

错误:

java.lang.SecurityException: Permission Denial: reading com.android.fileexplorer.provider.FileExplorerFileProvider uri content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Marbles.mp3 from pid=1874, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:616)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:483)
        at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:474)
        at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:419)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:313)
        at android.os.Binder.execTransact(Binder.java:569)
2018-11-11 16:08:20.613 756-15193/? E/MediaPlayerService: Couldn't open fd for content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Marbles.mp3
2018-11-11 16:08:20.613 1874-23378/? E/MediaPlayer: Unable to create media player

minSdkVersion 21

如何解决?

UPD:

我在“ Uri声音= Uri.parse(account.notifySound);”之后添加:

 grantUriPermission("com.android.systemui", sound, Intent.FLAG_GRANT_READ_URI_PERMISSION); 

出现错误:

 java.lang.SecurityException: Uid 10204 does not have permission to uri 0 @ content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Marbles.mp3

UPD2:

铃声选择器打开了多个声音浏览器,例如“ Music”,“ Themes”,再次是“ Music”-另一个声音浏览器,与第一个“ Music”不同...

当我选择“音乐”中的声音时,选择了soundUri =:

  

content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Snapchat_Tone-04a0409e-68c4-4294-beb3-bb137b8d5886.mp3

在“主题”中选择uri时选择声音:

  

file:///storage/emulated/0/MIUI/.ringtone/Snapchat_Tone-04a0409e-68c4-4294-beb3-bb137b8d5886.mp3

当Uri以“ file:///”(声音资源管理器“主题”)开头时播放此声音,而当Uri以“ content://”(声音资源管理器“音乐”)开头时出错。

在用Uri选择“音乐”时,尽管播放了其他一些文件,但文件的开头是“ content:// ...”

1 个答案:

答案 0 :(得分:0)

如编译器所说:

  

需要导出提供程序,或grantUriPermission()

您可以尝试以下方法:

grantUriPermission("com.android.systemui", sound,
    Intent.FLAG_GRANT_READ_URI_PERMISSION);

其中sound是您与setSound()一起使用的Uri

如果您不能确定应授予哪些软件包许可权,则在以下助手功能下将循环搜索:

public static void grantUriPermission(Activity ctx, Intent intent, Uri uri, int permissionFlags) {
    List<ResolveInfo> resolvedIntentActivities = ctx.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
        String packageName = resolvedIntentInfo.activityInfo.packageName;

        ctx.grantUriPermission(packageName, uri, permissionFlags);
    }
}