我正在更改我的应用程序代码以支持 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_PERMISSION
与 Uri
我们用于定制的Notification
声音。Stay tuned for further developments .
关于java - 来自文件提供程序 Uri 的 Android 7.0 通知声音未播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39359465/