gpt4 book ai didi

android - 如何使用任何 "not Activity"中的铃声选择器(无 Activity)

转载 作者:行者123 更新时间:2023-11-29 14:01:06 26 4
gpt4 key购买 nike

有没有办法在没有 Activity 的情况下使用铃声选择器?在我的案例中,这是来自 DialogPreference。

由于DialogPreference不是Activity,这里有两个问题:

  1. 没有startActivityForResult
  2. 没有 onActivityResult 方法。

我设法通过将构造函数中接收到的 Context 转换为 Activity 来使用 startActivityForResult,但我找不到捕获任何结果的方法。

作为变通方法,我使用来自 RingtoneManager 的铃声光标创建了带有 singleChoiceItems 设置的自定义 AlertDialog,但这不包括“默认铃声”、选定的铃声、不播放音乐,只是与默认铃声选择器“感觉不同”。

总而言之 - 是否有任何方法可以从 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;
}

最佳答案

您需要的这两种方法都是 Context 类的一部分。只需将 getApplicationContext 传递到需要它的地方即可。

关于android - 如何使用任何 "not Activity"中的铃声选择器(无 Activity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9481273/

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