有没有办法在没有活动的情况下使用铃声选择器? 就我而言,这是来自DialogPreference。
由于DialogPreference不是Activity,因此这里有两个问题:
我设法通过将在contructor中收到的Context转换为Activity来使用startActivityForResult,但我找不到捕获任何结果的方法。
作为一种解决方法,我创建了自定义AlertDialog,其中singleChoiceItems使用来自RingtoneManager的铃声'Cursor设置,但不包括“默认铃声”,选定的铃声,不播放音乐,只是与默认铃声选择器“感觉不同”。
总结一下 - 有没有办法从DialogPreference运行铃声选择器,还是需要创建整个自定义对话框?
编辑: 这是我提出的自定义铃声选择器解决方案(问题仍然存在 - 如何使用默认铃声选择器?)。它有一些缺点(特别是临时路径 - 如何修复它?),但可能对某人有用
// all this is inside some Dialog or other non Activity
Context mContext;
// that is the string I want to get from Ringtone picker
// something like content://media/internal/audio/media/60
// I can also get it stored version from somewhere else (preferences and such)
private String mRingtonePath = null;
// that is temp path I am using, because I can't find other way to store path
// received in setSingleChoiceItems onClickListener
private String mRingtoneTempPath = null;
void showRingtoneDialog() {
RingtoneManager rm = new RingtoneManager(mContext);
final Cursor ringtones = rm.getCursor();
final MediaPlayer mp = new MediaPlayer();
int selected = -1;
// moving to proper ringtone in case some path was already supplied
if (mRingtonePath != null)
for (ringtones.moveToFirst(); !ringtones.isAfterLast(); ringtones
.moveToNext()) {
selected++;
String path = ringtones
.getString(RingtoneManager.URI_COLUMN_INDEX)
+ "/"
+ ringtones.getInt(RingtoneManager.ID_COLUMN_INDEX);
if (path.equals(mRingtonePath)) {
break;
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("TITLE");
builder.setSingleChoiceItems(ringtones, selected,
ringtones.getColumnName(RingtoneManager.TITLE_COLUMN_INDEX),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ringtones.moveToPosition(which);
String path = ringtones
.getString(RingtoneManager.URI_COLUMN_INDEX)
+ "/"
+ ringtones
.getInt(RingtoneManager.ID_COLUMN_INDEX);
// ugly solution to store temp path
setTempPathTo(path);
mp.reset();
try {
Uri uri = Uri.parse(path);
mp.setDataSource(mContext, uri);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mp.reset();
mp.release();
// I could read path nicely from here instead of using temp path,
// but ringtones Cursor somehow moves couple positions forward
// since last call to onClick in setSingleChoiceItems and
// String s = ringtones
// .getString(RingtoneManager.URI_COLUMN_INDEX)
// + "/"
// + ringtones.getInt(RingtoneManager.ID_COLUMN_INDEX);
mRingtonePath = mRingtoneTempPath;
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mp.reset();
mp.release();
}
});
AlertDialog alert = builder.create();
alert.show();
}
void setTempPathTo(String path) {
mRingtoneTempPath = path;
}
答案 0 :(得分:0)
您需要的两种方法都是Context类的一部分。只需将getApplicationContext传递到需要的位置即可。
答案 1 :(得分:0)
您可以使用RingtonePreference
获取铃声,而不是使用自定义对话框。
例如,将其放在您的偏好
中<RingtonePreference
android:key="ringtone"
android:title="Ringtone Preference"
android:showDefault="true"
android:showSilent="true"
android:summary="Pick a tone, any tone"
/>
通过
引用它SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String s = prefs.getString("ringtone", "<unset>");
我也想说谢谢你的代码,我把它用于我需要的自定义铃声对话框。