java - 来自文件提供程序 Uri 的 Android 7.0 通知声音未播放

标签 java android android-studio android-fileprovider android-7.0-nougat

我正在更改我的应用程序代码以支持 Android 7,但是在从 FileProvider 传递 Uri 的 NotificationCompat.Builder.setSound(Uri) 中,通知不播放任何声音,在 Android 6 中使用 Uri.fromFile() 可以正常工作。

mp3 文件位于:

/Animeflv/cache/.sounds/



这是我的通知代码:

knf.animeflv.RequestBackground


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_not_r)
.setContentTitle(NotTit)
.setContentText(mess);
...
mBuilder.setVibrate(new long[]{100, 200, 100, 500});
mBuilder.setSound(UtilSound.getSoundUri(not)); //int

这是我的 UtilSound.getSoundUri(int)
public static Uri getSoundUri(int not) {
        switch (not) {
            case 0:
                return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            default:
                try {
                    File file=new File(Environment.getExternalStorageDirectory()+"/Animeflv/cache/.sounds",getSoundsFileName(not));
                    if (file.exists()) {
                        file.setReadable(true,false);
                        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
                            return FileProvider.getUriForFile(context, "knf.animeflv.RequestsBackground",file);
                        }else {
                            return Uri.fromFile(file);
                        }
                    }else {
                        Log.d("Sound Uri","Not found");
                        return getSoundUri(0);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    return getSoundUri(0);
                }
        }
    }

在 AndroidManifest.xml 中:
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="knf.animeflv.RequestsBackground"
    android:exported="false"
    android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
</provider>

provider_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="_.sounds" path="Animeflv/cache/.sounds/"/>
</paths>

最佳答案

以下来自a blog post我刚刚发表的,在这里转载,因为,嘿,为什么不呢?

您可以在 Notification 上设置自定义铃声,通过类似的方法setSound()NotificationCompat.Builder .这需要一个 Uri , 和
据报道,这会导致 Android 7.0 出现问题
a few people
Stack Overflow .

如果您使用 file: Uri值,它们不再适用于 Android 7.0
如果您的 targetSdkVersion为 24 或更高,如声音 Uri被检查
符合 the ban on file: Uri values .

但是,如果您尝试使用 content: Uri例如,来自 FileProvider , 您的
声音不会播放...因为Android没有读取权限
内容。

以下是解决此问题的一些选项。

手术刀:grantUriPermissions()

您始终可以通过 grantUriPermissions() 将内容权限授予其他应用程序,Context 上提供的方法.挑战在于知道该找谁
授予权限。

在 Nexus 6P(Android 6.0...仍然...)和 Nexus 9(Android 7.0)上有效的是:

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

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

这是否适用于所有设备和所有 Android 操作系统版本,
我不能说。

断头台:不再有用户文件
android.resource作为一个方案适用于Uri setSound() 的值.
而不是允许用户从文件中选择自己的铃声,
您只允许他们选择您发送的几种铃声之一
作为应用程序中的原始资源。如果这代表应用程序丢失
功能,但是,您的用户可能不为所动。

斧头:使用自定义 ContentProvider
FileProvider导出时无法使用 - 它崩溃
启动。但是,对于这种情况,唯一的 content: Uri那将
没有其他问题的工作是提供者是 exported
没有读取访问权限(或碰巧需要一些权限com.android.systemui或等价物恰好成立)。

最终,我将为此添加选项
my StreamProvider , 作为
一些“只读”提供程序功能的一部分。

但是,您可以为此推出自己的提供商。

电锯:禁令

以下代码 fragment 阻止所有 StrictMode相关检查
VM 行为(即主应用程序线程行为以外的东西),
包括对 file: 的禁令Uri值(value)观:
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());

或者,您可以配置自己的 VmPolicy随便
您想要的规则,无需调用 detectFileUriExposure() .

这允许您使用 file: Uri任何地方的值(value)观。有好的
Google 禁止 file: 的原因Uri ,因此试图避免
从长远来看,禁令可能会咬你不幸的 body 部位。

Nuke:使用较低的 targetSdkVersion

这也取消了对 file: 的禁令。 Uri值,以及所有其他targetSdkVersion 的行为24+ 选择加入。值得注意的是,这将
导致您的应用显示“可能不适用于分屏”Toast如果用户进入分屏多窗口模式。

真正的解决方案:Android 中的修复
NotificationManager应该调用grantUriPermissions()对我们来说,或者应该有一些其他的方式让我们联系FLAG_GRANT_READ_URI_PERMISSIONUri我们用于定制的Notification声音。
Stay tuned for further developments .

关于java - 来自文件提供程序 Uri 的 Android 7.0 通知声音未播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39359465/

相关文章:

Eclipse 中带条件的 Java 异常断点

android - AndEngine-停止所有声音和音乐运行时-更改引擎的AudioOptions

c# - Xamarin 从位图中获取错误的颜色

android - 依赖性搞砸了 Flutter 中的 Android 应用程序构建

java - 使用 Metro 的 Ws-Security header

java - Net.sf.jasperreports.engine.util.JRStyledTextParser 上的 JasperReports NoClassDefFoundError 异常

java - Netbeans 中的 Ant : overriding run target doesn't work

android - 如何向 ListView 中的 arraylist 项添加操作以及如何获取其位置?

android-studio - Android Studio 将物理设备识别为 Null?

android - 从 Android Studio 浏览 SQLite 数据库