显示设置:
一:直接定位到onCreate()
<pre name="code" class="java">@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver resolver = getActivity().getContentResolver();//还是获取contentresover对象
addPreferencesFromResource(R.xml.display_settings);//加载数据
--------------------------------------------------------------------------------------------------------------
mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
mAccelerometer.setPersistent(false);
------------------------------------------------------------------------------------------------------------1:实例化一个checkBox
if (!RotationPolicy.isRotationSupported(getActivity())
|| RotationPolicy.isRotationLockToggleSupported(getActivity())) {
// If rotation lock is supported, then we do not provide this option in
// Display settings. However, is still available in Accessibility settings,
// if the device supports rotation.
getPreferenceScreen().removePreference(mAccelerometer);
}
------------------------------------------------------------------------------------------------------------如果支持旋转锁不提供这个选项
mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
if (mScreenSaverPreference != null
&& getResources().getBoolean(
com.android.internal.R.bool.config_dreamsSupported) == false) {
getPreferenceScreen().removePreference(mScreenSaverPreference);
}
-------------------------------------------------------------------------------------------------------------2:实例化一个fragment,满足条件就移除
mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT,//从数据库取值
FALLBACK_SCREEN_TIMEOUT_VALUE);
long timeoutValue = (currentTimeout == Integer.MAX_VALUE) ? -1 : currentTimeout;
mScreenTimeoutPreference.setValue(String.valueOf(timeoutValue));
mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
disableUnusableTimeouts(mScreenTimeoutPreference);
updateTimeoutPreferenceDescription(timeoutValue);
------------------------------------------------------------------------------------------------------------3:实例化一个listPreference
mFontSizePref = (WarnedListPreference) findPreference(KEY_FONT_SIZE);
<span style="white-space:pre"> </span>mFontSizePref.setOnPreferenceChangeListener(this);
mFontSizePref.setOnPreferenceClickListener(this);
------------------------------------------------------------------------------------------------------------4:实例化设置字体大小的
mNotificationPulse = (CheckBoxPreference) findPreference(KEY_NOTIFICATION_PULSE);
if (mNotificationPulse != null
&& getResources().getBoolean(
com.android.internal.R.bool.config_intrusiveNotificationLed) == false) {
getPreferenceScreen().removePreference(mNotificationPulse);
} else {
try {
mNotificationPulse.setChecked(Settings.System.getInt(resolver,
Settings.System.NOTIFICATION_LIGHT_PULSE) == 1);
mNotificationPulse.setOnPreferenceChangeListener(this);
} catch (SettingNotFoundException snfe) {
Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found");
}
}
------------------------------------------------------------------------------------------------------------5:实例化checkBox
mCablBrightnessPreference = (CheckBoxPreference) findPreference(KEY_CABL_BRIGHTNESS);
mCABLServiceWrapper = new CABLServiceWrapper(this.getActivity());
if (!mCABLServiceWrapper.isCABLAvailable()) {
getPreferenceScreen().removePreference(mCablBrightnessPreference);
} else {
mCABLServiceWrapper.initCABLService();
}
------------------------------------------------------------------------------------------------------------6:实例化checkBox
</pre>二:来看一下XML文件中的布局Display_settings<pre name="code" class="java"><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/display_settings"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
-------------------------------------------
<com.android.settings.BrightnessPreference
android:title="@string/brightness"
android:persistent="false"/>
-------------------------------------------------------//上面是自己定义的两度类
<CheckBoxPreference
android:key="cabl_brightness"
android:title="@string/enhanced_brightness"
android:summary="@string/enhanced_brightness_summary"/>
---------------------------------------------------------------//cabl亮度
<PreferenceScreen
android:persistent="false"
android:key="screencolor_settings"
android:title="@string/screencolor">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.android.settings"
android:targetClass="com.android.settings.ScreenColorSettings" />
</PreferenceScreen>
--------------------------------------------------------------------------------------------屏幕颜色设置
<PreferenceScreen
android:key="wallpaper"
android:title="@string/wallpaper_settings_title"
android:fragment="com.android.settings.WallpaperTypeSettings" />
-------------------------------------------------------------------------------------------壁纸的设置fragment
<CheckBoxPreference
android:key="accelerometer"
android:title="@string/accelerometer_title"/>
---------------------------------------------------------------------------旋转屏幕的checkBox
<ListPreference
android:key="screen_timeout"
android:title="@string/screen_timeout"
android:summary="@string/screen_timeout_summary"
android:persistent="false"
android:entries="@array/screen_timeout_entries"
android:entryValues="@array/screen_timeout_values" />
-----------------------------------------------------------------------------list类型的休眠
<PreferenceScreen
android:key="screensaver"
android:title="@string/screensaver_settings_title"
android:fragment="com.android.settings.DreamSettings" />
----------------------------------------------------------------------------fragment互动屏保
<com.android.settings.WarnedListPreference
android:key="font_size"
android:title="@string/title_font_size"
android:summary="@string/summary_font_size"
android:entries="@array/entries_font_size"
android:entryValues="@array/entryvalues_font_size"
android:dialogTitle="@string/dialog_title_font_size" />
----------------------------------------------------------------------------字体大小的设置
<CheckBoxPreference
android:key="notification_pulse"
android:title="@string/notification_pulse_title"
android:persistent="false" />
------------------------------------------------------------收到通知指示灯闪烁
<span style="white-space:pre"> <PreferenceScreen
android:key="wifi_display"
android:title="@string/wifi_display_settings_title"
android:fragment="com.android.settings.wfd.WifiDisplaySettings" /></span>投射屏幕
</PreferenceScreen>
三:定位到onCreateView()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_layout_preference,null);
return v;
}
四:定位onResume()
@Override
public void onResume() {
super.onResume();
RotationPolicy.registerRotationPolicyListener(getActivity(),
mRotationPolicyListener);
<span style="color:#505050;">主要更新了当前的配置项目,自动旋转屏幕,字体大小,互动屏保</span>
updateState();
}
五:再看看单机事件onPreferenceTreeClick,当
onPreferenceClick返回的是flase则调用这个方法
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference == mAccelerometer) {
RotationPolicy.setRotationLockForAccessibility(
getActivity(), !mAccelerometer.isChecked());
} else if (preference == mNotificationPulse) {
boolean value = mNotificationPulse.isChecked();
Settings.System.putInt(getContentResolver(), Settings.System.NOTIFICATION_LIGHT_PULSE,//指示灯选择
value ? 1 : 0);
return true;
} else if (preference == mCablBrightnessPreference) {
final boolean checked = mCablBrightnessPreference.isChecked();//cal亮度
if (checked) {
mCABLServiceWrapper.startCABL();
} else {
mCABLServiceWrapper.stopCABL();
}
}
return super.onPreferenceTreeClick(preferenceScreen, preference);//调用父类的方法,启动fragment,或者是启动activity
}
看看
onPreferenceClick,先走
onPreferenceClick,再走
onPreferenceTreeClick,当下面的返回为true就不走上面的了,因为字体的先设置
@Override
public boolean onPreferenceClick(Preference preference) {
if (preference == mFontSizePref) {
if (Utils.hasMultipleUsers(getActivity())) {
showDialog(DLG_GLOBAL_CHANGE_WARNING);//主要是设置字体
return true;
} else {
mFontSizePref.click();
}
}
return false;
}<pre name="code" class="java">protected void showDialog(int dialogId) {
if (mDialogFragment != null) {
Log.e(TAG, "Old dialog fragment not null!");
}
mDialogFragment = new SettingsDialogFragment(this, dialogId);
mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
}
</pre><p></p><pre>
六:就都分析完了,大概的这个界面的流程,再看里面的每个界面的分析
一:亮度BrightnessPreference这个类
public class BrightnessPreference extends Preference {
public BrightnessPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onClick() {
Intent intent = new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);
getContext().sendBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF);发送一个广播显示亮度调节
}
}
二:壁纸WallpaperTypeSettings这个类
public class WallpaperTypeSettings extends SettingsPreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.wallpaper_settings);
populateWallpaperTypes();
}
private void populateWallpaperTypes() {
// Search for activities that satisfy the ACTION_SET_WALLPAPER action
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
final PackageManager pm = getPackageManager();
List<ResolveInfo> rList = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
final PreferenceScreen parent = getPreferenceScreen();
parent.setOrderingAsAdded(false);
// Add Preference items for each of the matching activities下面就是给每个条目加动态添加,原来自数据库查询出来的
for (ResolveInfo info : rList) {
Preference pref = new Preference(getActivity());
Intent prefIntent = new Intent(intent);
prefIntent.setComponent(new ComponentName(
info.activityInfo.packageName, info.activityInfo.name));
pref.setIntent(prefIntent);
CharSequence label = info.loadLabel(pm);
if (label == null) label = info.activityInfo.packageName;
pref.setTitle(label);
parent.addPreference(pref);
}
}
}
三:休眠是个listpreference
四:字体大小显示dialog