設(shè)置中的Preferencescreen不僅可以作為設(shè)置界面顯示,,而且還能夠啟動(dòng)activity,,下面主要是對(duì)啟動(dòng)activity的介紹 1. Preferencescreen中啟動(dòng)activity 例如wireless_setting.xml中有如下片段
<PreferenceScreen xmlns:android="http://schemas./apk/res/android" xmlns:settings="http://schemas./apk/res/com.seedshope.android"> <PreferenceScreen android:key="wifi_settings" android:title="@string/wifi_settings" android:summary="@string/wifi_settings_summary" > <intent android:action="android.intent.action.MAIN" android:targetPackage="com.android.settings" android:targetClass="com.android.settings.wifi.WifiSettings" /> </PreferenceScreen> </PreferenceScreen>
其中com.android.settings是工程的包名,,com.android.settings.wifi.WifiSettings是要啟動(dòng)的類。 一般情況下,,這樣就ok了,,點(diǎn)擊對(duì)應(yīng)的Preference就能夠啟動(dòng)對(duì)應(yīng)的activity,然而僅僅這樣有時(shí)這樣,,并不一定能夠啟動(dòng)對(duì)應(yīng)的activity,,因?yàn)樗c回調(diào)函數(shù)onPreferenceTreeClick有關(guān),有時(shí)候我們會(huì)重寫這個(gè)方法如: public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { if ( (preference == mAirplaneModePreference) && (Boolean.parseBoolean( SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) ) { // In ECM mode launch ECM app dialog startActivityForResult( new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null), REQUEST_CODE_EXIT_ECM); return true; } else { // Let the intents be launched by the Preference manager return false; } } 其中WirelessSetting.java中有如上函數(shù),當(dāng)返回值為true時(shí),你怎么點(diǎn)擊Preference都不會(huì)跳轉(zhuǎn)到activity的,,只有等到它的返回值為false的時(shí)候,,才可以正常跳轉(zhuǎn),因?yàn)辄c(diǎn)擊某一個(gè)Preference的真正實(shí)現(xiàn)在Preference.java的performClick函數(shù)中如下: void performClick(PreferenceScreen preferenceScreen) { if (!isEnabled()) { return; } onClick(); if (mOnClickListener != null && mOnClickListener.onPreferenceClick(this)) { return; } PreferenceManager preferenceManager = getPreferenceManager(); if (preferenceManager != null) { PreferenceManager.OnPreferenceTreeClickListener listener = preferenceManager .getOnPreferenceTreeClickListener(); if (preferenceScreen != null && listener != null && listener.onPreferenceTreeClick(preferenceScreen, this)) { return; } } if (mIntent != null) { Context context = getContext(); context.startActivity(mIntent); } } 當(dāng)點(diǎn)擊某個(gè)Preference時(shí)的調(diào)用流程是AdapterView.performItemClick--》PreferenceScreen.onItemClick--》Preference.performClick--》PreferenceActivity.onPreferenceTreeClick,,當(dāng)onPreferenceTreeClick返回true的時(shí)候就直接return了,沒有走下面啟動(dòng)activity的地方了,,因此要使一個(gè)Preference能夠正常跳轉(zhuǎn)到另外一個(gè)activity有兩個(gè)條件,,一是xml里面是否設(shè)置正確,第二是調(diào)用該xml的java類是否在onPreferenceTreeClick這個(gè)函數(shù)需要返回false,。
|