// just define a unique int for this ringtone picker activity
public static final int RINGTONE_PICKER_REQUEST = 10;
void showRingtonePicker() {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
Activity activity =getActivity();
if (activity == null) {
return;
}
try {
activity.startActivityForResult(intent, RINGTONE_PICKER_REQUEST);
} catch (ActivityNotFoundException ignored) {
Logcat.e(ignored, "Settings showRingtonePicker error");
}
}
To get the result of ringtone picker, override onActivityResult in Activity.
@Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != RINGTONE_PICKER_REQUEST || resultCode != Activity.RESULT_OK) {
return false;
}
Uri ringtoneUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
// if user choose "no ringtone" in the ringtone picker, the ringtoneUri would be null.
String ringtoneUriString = "";
if (uri != null) {
ringtoneUriString = uri.toString();
}
// TODO : save the ringtoneUri or ringtoneUriString to anywhere you want
return true;
}