【问题标题】:Notification sound not played. Permission Denial未播放通知声音。权限拒绝
【发布时间】:2018-11-11 09:19:55
【问题描述】:

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

代码:

    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 sound = 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:

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

当我在“音乐”中选择声音时,选择 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://...”

【问题讨论】:

  • Android 现在是我见过的漏洞最多、最不可靠的操作系统...

标签: android notifications


【解决方案1】:

正如编译器所说:

要求提供者被导出,或者 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);
    }
}

【讨论】:

  • 我添加了 grantUriPermission 并得到了错误。我更新了我的问题
猜你喜欢
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 2019-04-11
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
相关资源
最近更新 更多