标签:har checkbox public install drawable private odi amp 调用
由于要改一些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(); }
那么为啥有的header还是用intent指定activity呢。我分析是由于有些系统界面须要别的应用直接intent訪问。比方WIFI设置界面:
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); }
首选项框架PreferenceFragment部分源代码分析
标签:har checkbox public install drawable private odi amp 调用
原文地址:http://www.cnblogs.com/lxjshuju/p/6854782.html