Today, we mainly study the Preferencescreen application in the setup, it can not only display as the setting interface, but also start the activity, the following is mainly the introduction of the start activity
1. Start activity in Preferencescreen
For example, the following fragments are found in Wireless_setting.xml
<preferencescreen xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:settings= "Http://schemas.android.com/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 is the package name for project, and Com.android.settings.wifi.WifiSettings is the class to start.
Under normal circumstances, this will be OK, click on the corresponding preference to start the corresponding activity, but this sometimes, not necessarily can start the corresponding activity, because it is related to the callback function Onpreferencetreeclick, Sometimes we will rewrite this method such as:
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 is launched by the Preference Manager
return false;
}
}
In Wirelesssetting.java, as in the function, when the return value is True, you can click on the preference will not jump to the activity, just wait until its return value is false, talent enough to jump normally, Because clicking on one of the preference's true real-preference.java performclick functions, for example, is as follows:
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);
}
}
When you click on a Preference the call flow is adapterview.performitemclick--"preferencescreen.onitemclick--" preference.performclick--" Preferenceactivity.onpreferencetreeclick, when Onpreferencetreeclick returns True, return directly, not the following to start the activity of the place, so to make a preference can Normal jump to another activity has two conditions, one is whether the XML is set correctly, and the second is whether the Java class calling the XML is onpreferencetreeclick this function needs to return FALSE.
Introduction and analysis of Preferencescreen usage methods in Android settings