[英]How to store user selected Ringtone in android studio?
public void onRingtone(View view) {
final Uri currentTone= RingtoneManager.getActualDefaultRingtoneUri(Setting.this, RingtoneManager.TYPE_ALARM);
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Ringtone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
startActivityForResult(intent, 999);
}
I have this onClick
method to select available Ringtone from it's own device.我有这个onClick
方法可以从它自己的设备中选择可用的铃声。 When startActivityForResult()
method is called it redirects to custom popup dialog from where user can select a Ringtone and at same time it starts that Ringtone.当调用startActivityForResult()
方法时,它会重定向到自定义弹出对话框,用户可以从中选择铃声,同时启动该铃声。 That's Ok.没关系。 But I want to save that Ringtone to play it on another activity.但我想保存该铃声以在其他活动中播放。 So what should I do?所以我该怎么做? Can I store it in database(SQLite), or in SharedPreference or any other way?我可以将它存储在数据库(SQLite)、SharedPreference 或任何其他方式中吗?
Edit: I don't know why my question got closed even if I provided all information.编辑:即使我提供了所有信息,我也不知道为什么我的问题被关闭了。 But still giving more information, to understand my question better.但仍然提供更多信息,以更好地理解我的问题。
Xml file: xml文件:
<RelativeLayout
android:id="@+id/Ringtone"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@id/sp2"
android:layout_marginTop="1dp"
android:background="@drawable/pressed"
android:clickable="true"
android:focusable="true"
android:onClick="onRingtone"
android:gravity="center"
android:paddingTop="15dp"
android:paddingBottom="15dp">
The above method is called when the user selects the Ringtone.当用户选择铃声时调用上述方法。 I got one answer which told me to use SharedPreference to store uri.我得到了一个答案,它告诉我使用 SharedPreference 来存储 uri。 I want to keep the selected ringtone even after app gets closed.即使在应用程序关闭后,我也想保留选定的铃声。 So SharedPreference is a better option but how to make it global so other activity can also use it?所以 SharedPreference 是一个更好的选择,但如何使它成为全球性的以便其他活动也可以使用它?
If I create object of SharedPreference in this java file, how can I get access to other acivity?如果我在这个 java 文件中创建 SharedPreference 的对象,我怎样才能访问其他活动?
To save ring tone Uri , you can use:要保存铃声 Uri ,您可以使用:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("currentTone", currentTone.toString());
editor.commit();
To retrieve ring tone Uri you need to parse the string to Uri :要检索铃声 Uri,您需要将字符串解析为 Uri :
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String currentToneString= settings.getString("currentTone", null);
Uri currentTone= Uri.parse(currentToneString);
return currentTone;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.