How to add application notification sound to notification list?

I want the user to be able to select a notification sound for my application, so I use the following code:

Intent ringtoneIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false); ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false); ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Choose"); ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null); fragment.startActivityForResult(ringtoneIntent, REQUEST_RINGTONE); 

Unfortunately, I do not have my own application notification sound in the list. The list includes Google Hangouts, Calendar, and Facebook. I assume that these programs did something to register in android, but I can not find documentation on how to do this.

+5
source share
2 answers

First copy the file to the rigntones folder (see Environment.DIRECTORY_RINGTONES)

Then register the sound:

 ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, nameOfSound); values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); values.put(MediaStore.Audio.Media.ARTIST, yourAppName); values.put(MediaStore.Audio.Media.IS_RINGTONE, true); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); values.put(MediaStore.Audio.Media.IS_ALARM, true); values.put(MediaStore.Audio.Media.IS_MUSIC, false); Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()); getContentResolver().insert(uri, values); 
+3
source

One very simple and simple solution would be to simply copy your sound files to Environment.DIRECTORY_NOTIFICATIONS or Environment.DIRECTORY_RINGTONES .

0
source

Source: https://habr.com/ru/post/1214024/


All Articles