Setting sound for notification

霸气de小男生 提交于 2019-12-10 09:27:29

问题


How can I set sound for notification for my android application. In my application notification will be shown after 30 seconds. I want to give options for this alerts such as silent mode, vibration mode and an option to select from the available tones from the device. I am using the preference screen to show the settings menu. I want to to set the notification ring type application specific. Is there any way to establish this..


回答1:


http://developer.android.com/reference/android/app/Notification.Builder.html#setSound(android.net.Uri)

Notification.Builder.setSound();

Use a ringtone preference in the preference activity to get the URI of the selected sound.




回答2:


How to Set a Custom Sound for an Android Notification

Place a .mp3 or .wav file in your res/raw directory such as "notification_sound.mp3" as in the example below (the filename must not use capital letters).

Set the Notification.sound when you create your Notification, like this:

final int iconResId = R.drawable.my_icon;
final int soundResId = R.raw.notification_sound;
final Notification notification =
    new Notification(iconResId, tickerText, System.currentTimeMillis());
final String packageName = context.getPackageName();
notification.sound =
    Uri.parse("android.resource://" + packageName + "/" + soundResId);

Optionally, add vibration to your Notification:

notification.defaults = Notification.DEFAULT_VIBRATE;


来源:https://stackoverflow.com/questions/5139919/setting-sound-for-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!