标签:
因为要改一些settings里面的bug以及之前在里面有做过勿扰模式,准备对勿扰模式做一个总结,那先分析一下settings的源码,里面的核心应该就是android3.0 上面的首选项框架PreferenceFragment。因为在3.0之前都是把这些东西放在PreferenceActivity的,但是3.0之后google建议把setting放在PreferenceFragment,但是PreferenceActivity也同时在用的,下面就此总结一下:
PreferenceActivity的作用是管理所有headers,而每一个header对应一个PreferenceFragment,对应不同的设置种类界面所以我感觉PreferenceActivity偏向系统级的设置,就比如android系统的settings,对于一些简单的应用设置一个PreferenceFragment是可以搞定的。
我们自定义一个 activity extends PreferenceActivity必须重写onBuildHeaders()这个方法,在这里加载一个xml布局显示:
@Override public void onBuildHeaders(List<Header> target) { // TODO Auto-generated method stub loadHeadersFromResource(R.xml.activity_headers, target); }
<?xml version="1.0" encoding="utf-8"?> <preference-headers xmlns:android="http://schemas.android.com/apk/res/android" > <header android:id="@+id/wifi" android:title="wifi" android:icon="@drawable/ic_launcher" android:fragment="com.example.preferencefragmenttest.WifiFragment"/> <header android:id="@+id/net" android:title="移动网络" android:icon="@drawable/ic_launcher" android:fragment="com.example.preferencefragmenttest.NetFragment"/> <header android:id="@+id/ee" android:title="打开activity" android:icon="@drawable/ic_launcher" > <intent android:action="android.intent.action.NEW"></intent> </header> </preference-headers>
public void onHeaderClick(Header header, int position) { if (header.fragment != null) { if (mSinglePane) { int titleRes = header.breadCrumbTitleRes; int shortTitleRes = header.breadCrumbShortTitleRes; if (titleRes == 0) { titleRes = header.titleRes; shortTitleRes = 0; } startWithFragment(header.fragment, header.fragmentArguments, null, 0, titleRes, shortTitleRes); } else { switchToHeader(header); } } else if (header.intent != null) { startActivity(header.intent); } }
private void switchToHeaderInner(String fragmentName, Bundle args, int direction) { getFragmentManager().popBackStack(BACK_STACK_PREFS, FragmentManager.POP_BACK_STACK_INCLUSIVE); if (!isValidFragment(fragmentName)) { throw new IllegalArgumentException("Invalid fragment for this activity: " + fragmentName); } Fragment f = Fragment.instantiate(this, fragmentName, args); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.replace(com.android.internal.R.id.prefs, f); transaction.commitAllowingStateLoss(); }
startActivity(new Intent( android.provider.Settings.ACTION_WIRELESS_SETTINGS));
@Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.wifi_preference); }
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:key="isopen" android:title="wifi" android:defaultValue="true"/> <ListPreference android:key="list" android:title="声音大小" android:entries="@array/keys" android:entryValues="@array/values"/> <Preference android:key="click" android:title="单独的" /> </PreferenceScreen>
<PreferenceCategory > <cn.com.demo.ota.MyButtonPreference android:key="start_install" android:title="@string/update_install"/> </PreferenceCategory>
/** * {@inheritDoc} */ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { if (preference.getFragment() != null && getActivity() instanceof OnPreferenceStartFragmentCallback) { return ((OnPreferenceStartFragmentCallback)getActivity()).onPreferenceStartFragment( this, preference); } return false; }
update_more = (Preference)findPreference("update_more");<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"> </span></span></span><pre name="code" class="java">update_more.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { } });
当然上面说的都是最基础的用法,看settings的源代码里面的preference都是自定义的,用listview显示不同类型的preference,而且看Settings.java的一个方法:
/** * Populate the activity with the top-level headers. */ @Override public void onBuildHeaders(List<Header> headers) { loadHeadersFromResource(R.xml.iradar_settings_headers, headers); updateHeaderList(headers); }
标签:
原文地址:http://blog.csdn.net/dengliulin/article/details/44420179