如何在android中设置自定义声音的通知

我将mp3(kalimba.mp3)文件复制到res文件夹中的raw文件夹中。 但是,当通知被触发时,它会产生默认声音。

这是我如何通知:

 protected void GenerateNotify() { NotificationManager myNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification notification=new Notification(android.R.drawable.ic_btn_speak_now,"hi",100); Intent intent=new Intent(getApplicationContext(),as.class); PendingIntent contentintent=PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0); notification.setLatestEventInfo(getApplicationContext(), "Hi","date", contentintent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba); myNotificationManager.notify(NOTIFICATION_ID,notification); } 

Solutions Collecting From Web of "如何在android中设置自定义声音的通知"

 notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd); notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE; 

如果定义了DEFAULT_SOUND,那么默认声音将覆盖任何声音

R.raw.kalimba是一个整数资源ID; 你想在该Uri的声音资源的名称 。 所以试试:

 notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba"); 

尝试这个:

Uri sound = Uri.parse(“android.resource://”+ context.getPackageName()+“/ raw / notifysnd); notification.setSound(sound);