我如何制作铃声活动(总是出现在设置中),以便用户可以从系统铃声中选择她的铃声我用Google搜索但我没有找到完整的教程,我真的很困惑,请给我教程或一些代码
此外,如果我希望用户在我的应用程序中选择通知的特殊铃声,我应该使用共享偏好还是首选项?
我已经做了菜单:
// 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);
}
答案 0 :(得分:6)
完整代码:
<强> 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>
<强> Preferences.class 强>
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
您的代码转到此处:
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 强>
<activity
android:name=".Preferences"
android:label="@string/set_preferences">
</activity>
答案 1 :(得分:1)
是的,您可以使用SharedPreferences
存储用户选择的铃声的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);
以上代码将提示用户从系统中选择铃声。当他们选择一个时,您需要处理Activity
结果:
@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保存到SharedPreferences
。