android - 如何在android中使用自定义声音设置通知

标签 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);
}

最佳答案

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

if defined DEFAULT_SOUND, then the default sound overrides any sound

关于android - 如何在android中使用自定义声音设置通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13760168/

相关文章:

javascript - XHR 在 PhoneGap/Cordova + Android 4.1/4.2 + Samsung 硬件中失败

Android字体选择器示例?

android - 位图爆炸动画在一个地方旋转,不向外移动

java - 处理能力 - 移动设备与台式机 - 100 倍的差异?

java - 在 Android 中显示 ArrayList 中的数组列表

java - 运行时权限应该采用相同的方法。如何正确处理?

android - 如何让Android应用兼容不同的屏幕分辨率?

Android Dropbox Sync SDK 认证 View 未被关闭

android - 如何强制 BottomNavigationView 标签为多行

java - 如何在 AppCompatActivity.onCreate() 中膨胀 fragment 的布局?