来自URI解析的通知声音不起作用

deu*_*on0 2 android uri android-notifications

Stackoverflow上至少有7个与此相关的问题,我已多次尝试过每一个建议和解决方案,但没有一个有效.这是我最近的尝试:

private Notification createNotification() {
       Notification notification = new Notification();


       if(notifyImage=="food")
         {
           notification.icon = R.drawable.food;
           notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://com.example.memoryGuide/raw/start");
         }
       else
       {
           notification.icon = R.drawable.bar; 
           notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://com.example.memoryGuide/raw/start");
       }


       notification.when = System.currentTimeMillis();
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.flags |= Notification.FLAG_SHOW_LIGHTS;
       notification.defaults |= Notification.DEFAULT_VIBRATE;
       notification.defaults |= Notification.DEFAULT_LIGHTS; 
       notification.ledARGB = Color.WHITE;
       notification.ledOnMS = 1500;
       notification.ledOffMS = 1500;
       return notification;
 }
Run Code Online (Sandbox Code Playgroud)

您可以看到我尝试使用两次从不起作用的声音,但图标完美无缺.我不知道我是否遗漏了任何东西以使其工作,但我使用的所有代码都在我的帖子中.

我的声音文件在res/raw/start.mp3中,按下按钮我可以听到这个声音,所以声音很好.

我认为包名是正确的,我的应用程序在每个类的顶部都有:

package com.example.memoryGuide;
Run Code Online (Sandbox Code Playgroud)

任何想法为什么声音永远不会播放?

sti*_*ike 6

使用

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
        + "://" + getPackageName() + "/raw/start");
Run Code Online (Sandbox Code Playgroud)

请注意一件事.

字符串android.content.ContextWrapper.getPackageName()

返回你的android应用程序的包名称.和

package com.example.memoryGuide;

显示源包名称的包名称.