Currently I am just using a CheckBoxPreference to activate/deactivate sound. The sound is being played from a raw-resource.
Now I want to replace this CheckBoxPreference with a RingtonePreference, but my custom sound from my resources should be the default one and selectable from the ringtonepreference.
How can I do that?
i wrote a Preference Class which suits to my needs, here maybe it will help other peoples:
public class SoundPreference extends ListPreference implements
PreferenceManager.OnActivityResultListener, OnPreferenceChangeListener {
private NotificationPrefs mAct;
public SoundPreference(NotificationPrefs act) {
super(act);
mAct = act;
setEntries(new String[] { "Silent",
"Custom",
"Picker" });
setEntryValues(new String[] { "silent", "custom", "picker" });
setOnPreferenceChangeListener(this);
}
@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data
.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
persistString(uri.toString());
return true;
}
}
return false;
}
@Override
public void setValue(String value) {
List<CharSequence> vals = new ArrayList<CharSequence>(
Arrays.asList(getEntryValues()));
if (!vals.contains(value)) {
List<CharSequence> ents = new ArrayList<CharSequence>(
Arrays.asList(getEntries()));
Ringtone r = RingtoneManager.getRingtone(getContext(),
Uri.parse(value));
String name = r.getTitle(getContext());
vals.add(value);
ents.add(name);
this.setEntries(ents.toArray(new CharSequence[ents.size()]));
this.setEntryValues(vals.toArray(new CharSequence[vals.size()]));
}
super.setValue(value);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if ("picker".equals(newValue)) {
Intent i = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
i.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false);
i.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
Intent chooserIntent = Intent.createChooser(i, getContext()
.getString(R.string.sound));
mAct.startActivityForResult(chooserIntent, 0);
mAct.setActivityResultListener(this);
return false;
}
return true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.