gpt4 book ai didi

java - 使用 Intents 传递铃声数据

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:41 25 4
gpt4 key购买 nike

到目前为止,我一直在通过为 1 个文件创建 1 个 Activity 来制作具有设置为铃声功能的应用程序。这很糟糕,因为对于具有超过 20 个铃声的应用程序,我将需要 20 个会影响应用程序大小和性能的 Activity 。然后我发现有一种方法可以只用一个 Activity 和布局来做到这一点,通过 Intents 传递数据。现在我很清楚它是如何工作的,除了一件困扰我的事情。这就是我如何定义字符串。我需要 1 个字符串作为名称,1 个字符串作为文件路径

我的代码:

Boolean success = false;
rsound = new File(rpath, "Slow tone.mp3");rpath.mkdirs(); //Copied file name
if (!rsound.exists()) {




try {
InputStream in = getResources().openRawResource(R.raw.s8slowtone); //path for file
FileOutputStream out = new FileOutputStream(rsound.getPath());
byte[] buff = new byte[1024];
int read = 0;

try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();

out.close();
}
} catch (Exception e) {
success = false;

}
} else {
success = true;
setRingtone();

}

if (!success) {
setRingtone();


}
}

private void setRingtone() {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, rsound.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Slow tone"); //Ringtone name
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.Audio.Media.ARTIST, " ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, true);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(rsound.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + rsound.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
S15.this, RingtoneManager.TYPE_RINGTONE,
newUri);
Toast.makeText(getApplicationContext(), "Ringtone set successfully",
Toast.LENGTH_SHORT).show();

那么我该怎么做呢?如何为每个文件定义字符串以及如何传递它们?

由于某些成员的问题不清楚,我会使其更简单我不知道应该如何编写字符串,所以当我使用 Intent 启动 RingtoneManager Activity 时,我从字符串传递数据。那么我应该如何编写我的代码来传递这个

文件名“慢音.mp3”

文件路径:R.raw.s8slowtone)

铃声名称“慢调”

最佳答案

要调用您的 Activity ,只需将此函数放在任何地方,然后使用您想要的参数调用它。它将构建一个 Intent 并填写参数:

 public static void runRingToneActivity(Context context, String ringToneName, String ringTonePath, String ringToneFilename) {
Intent intent=new Intent(context, RingToneActivity.class);
intent.putExtra("NAME", ringToneName);
intent.putExtra("PATH", ringTonePath);
intent.putExtra("FILE", ringToneFileName);
((Activity)context).startActivity(intent);
}

在您的 RingToneActivity 的 onCreate 中,您只需检索刚刚传递的参数:

@Override
protected void onCreate(Bundle savedInstanceState) {

.
.


Intent intent=this.getIntent();

String ringtoneName=intent.getStringExtra("NAME");
String ringtonePath=intent.getStringExtra("PATH");
String ringtoneFile=intent.getStringExtra("FILE");

// you have them, now use them!

}

注意事项:

  • 如果您的 Activity 名称不同,请将函数中的“RingToneActivity.class”替换为“RingToneActivity.class”。

关于java - 使用 Intents 传递铃声数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22883315/

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