Java 我可以在“首选项”中制作铃声吗
我怎样才能使铃声活动(总是出现在设置中),以便用户可以选择她的铃声从系统铃声我谷歌它,但我没有找到完整的教程,我真的很困惑,请给我教程或一些代码 另外,如果我想让用户在我的应用程序中选择特殊的铃声通知,我应该使用共享首选项还是首选项 我已经做了菜单:Java 我可以在“首选项”中制作铃声吗,java,android,preference,ringtone,Java,Android,Preference,Ringtone,我怎样才能使铃声活动(总是出现在设置中),以便用户可以选择她的铃声从系统铃声我谷歌它,但我没有找到完整的教程,我真的很困惑,请给我教程或一些代码 另外,如果我想让用户在我的应用程序中选择特殊的铃声通知,我应该使用共享首选项还是首选项 我已经做了菜单: // Menu Code Part#2 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case
// Menu Code Part#2
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
case R.id.setting:
startActivity(new Intent(this, Setting.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
是的,您可以使用
SharedReferences
存储用户选择的铃声的URI。您可以使用以下方法让用户选择铃声:
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Ringtone");
if (mRingtoneUri != null) {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(mRingtoneUri));
} else {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
}
startActivityForResult(intent, RINGTONE_REQUEST);
上述代码将提示用户从系统中选择铃声。当他们选择一个时,您需要处理活动
结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RINGTONE_REQUEST && resultCode == RESULT_OK) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
SharedPreferences preferences = getSharedPreferences(PREF, MODE_PRIVATE);
Editor editor = preferences.edit();
if (uri == null)
editor.putString(RINGTONE, null);
else
editor.putString(RINGTONE, uri.toString());
editor.commit();
if (uri == null)
mRingtoneUri = null;
else
mRingtoneUri = uri.toString();
}
}
}
此代码将铃声的URI保存到SharedReferences
完整代码:
res/xml/preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Second Category">
<RingtonePreference
android:name="Ringtone Preference"
android:summary="Select a ringtone"
android:title="Ringtones"
android:key="ringtonePref" />
</PreferenceCategory>
</PreferenceScreen>
<activity
android:name=".Preferences"
android:label="@string/set_preferences">
</activity>
您的代码转到此处:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
// startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
case R.id.setting:
Intent settingsActivity = new Intent(getBaseContext(),
Preferences.class);
startActivity(settingsActivity);
return true;
default:
return super.onOptionsItemSelected(item);
}
要从代码中读取这些首选项,我们应该创建一个getPrefs()
方法,我们可以在onStart()
方法中调用该方法。当我们在onStart()
方法中调用它而不是在onCreate()
中调用它时,我们可以确保在设置了首选项并返回到主活动时加载它们,
getPrefs()
方法可能如下所示:
String ringtonePreference;
// Get the xml/preferences.xml preferences
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
ringtonePreference = prefs.getString("ringtonePref",
"DEFAULT_RINGTONE_URI");
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Second Category">
<RingtonePreference
android:name="Ringtone Preference"
android:summary="Select a ringtone"
android:title="Ringtones"
android:key="ringtonePref" />
</PreferenceCategory>
</PreferenceScreen>
<activity
android:name=".Preferences"
android:label="@string/set_preferences">
</activity>
您的代码将允许用户从系统中选择铃声,对吗?是的。您自己试试看。在活动中
您想让用户选择铃声的地方。谢谢,但是您能告诉我每个代码应该放在哪里吗?在活动中?还是在优惠活动中?谢谢你mainfist中的第一个代码?第二个代码应该放在newActivity中,我应该调用菜单中的newActivity?正当还有第二个代码,reingtone是完整的代码吗?还是我需要另一个代码?@user1257040:preferneces.xml文件中的第一个代码,以及您要访问选定铃声的第二个代码。它是完整的代码吗?我的意思是不需要另一个代码?嗨,朋友,你的问题解决了吗?这段代码在我这边运行得很好。好的,然后使用CheckBoxPreference并将这段代码添加到preferencs.xmle文件中