赞
踩
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
-
- <PreferenceCategory
- android:title="铃声设置">
- <RingtonePreference
- android:title="提示音"
- android:summary="默认铃声"
- android:key="key_ringtone_preference"
- android:ringtoneType="ringtone"/>
- </PreferenceCategory>
- </PreferenceScreen>
java代码:
- public class MainActivity extends Activity implements OnClickListener {
-
- private Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button) findViewById(R.id.button_settings);
- button.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- getFragmentManager().beginTransaction().replace(R.id.containner, new SettingPreference())
- .commit();
- }
- }
PreferenceFragment.java中:
- public class SettingPreference extends PreferenceFragment implements OnPreferenceChangeListener{
-
- private static Activity activity;
- private RingtonePreference mRingtone;
- private PreferenceScreen mPreferenceScreen;
- private static final String KEY_RINGTONE = "key_ringtone_preference";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.generalpreference);
- activity = getActivity();
- mPreferenceScreen = getPreferenceScreen();
- mRingtone = (RingtonePreference) mPreferenceScreen.findPreference(KEY_RINGTONE);
- String ringtoneUri = getRingtonePreference();
- String ringtone = getRingtoneTitleFromUri(ringtoneUri);
- mRingtone.setSummary(ringtone == null? "":ringtone);
- //设置监听事件
- mRingtone.setOnPreferenceChangeListener(this);
-
- }
- /**
- * 首选项值改变时回调
- */
- @Override
- public boolean onPreferenceChange(Preference preference, Object newValue) {
- // TODO Auto-generated method stub
- if(preference == mRingtone){
- Toast.makeText(getActivity(), "newValue="+newValue, Toast.LENGTH_SHORT).show();
- setRingtonePreference((String)newValue);
- String ringtongTitle = getRingtoneTitleFromUri((String)newValue);
- mRingtone.setSummary(ringtongTitle);
- }
- return true;
- }
-
- /**
- * 得到铃声的Uri
- * @return
- */
- public static String getRingtonePreference(){
-
- SharedPreferences prefs = activity.getSharedPreferences("SP", Context.MODE_PRIVATE);
- String ringtone = prefs.getString(KEY_RINGTONE, null);
- return ringtone;
-
- }
- /**
- * 将铃声的Uri进行存储
- * @param str
- */
- public static void setRingtonePreference(String str){
- SharedPreferences prefs = activity.getSharedPreferences("SP", Context.MODE_PRIVATE);
- prefs.edit().putString(KEY_RINGTONE, str).commit();
- }
-
- /**
- * 获取铃声名
- * @param uri
- * @return
- */
- public static String getRingtoneTitleFromUri(String uri){
- if(TextUtils.isEmpty(uri)){
- return null;
- }
- Ringtone ringtone = RingtoneManager.getRingtone(activity, Uri.parse(uri));
- String ringtoneTitle = ringtone.getTitle(activity);
- return ringtoneTitle;
-
- }
- }
运行效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。