gpt4 book ai didi

java - 带有来自原始资源的自定义铃声的 RingtonePreference

转载 作者:太空狗 更新时间:2023-10-29 13:29:33 28 4
gpt4 key购买 nike

目前我只是使用 CheckBoxPreference 来激活/停用声音。声音是从原始资源播放的。

现在我想将此 CheckBoxPreference 替换为 RingtonePreference,但我的资源中的自定义声音应该是默认声音,并且可以从 ringtonepreference 中选择。

我该怎么做?

最佳答案

我写了一个适合我需要的 Preference Class,在这里也许它会帮助其他人:

 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;

}
}

关于java - 带有来自原始资源的自定义铃声的 RingtonePreference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17340607/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com