Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add my app's notification sound to the notification sound list?

I want the user to be able to choose a notification sound for my app so I use the code below:

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, my app's own notification sound isn't in the list. Google Hangouts, Calendar, and Facebook are in the list. I assume these programs have done something to register with android, but I cannot find any documentation on how to do this.

like image 307
qtip Avatar asked Feb 23 '15 18:02

qtip


1 Answers

First copy your 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);
like image 171
yoah Avatar answered Sep 24 '22 03:09

yoah