Settings修改一:增加Switch控键

        系统设置一级菜单上wifi和蓝牙有个Switch按键,而且它和二级菜单上的Switch是同步的。而开发者选项菜单只是在二级菜单上有Switch控键,一级没有,于是产生了在一级菜单上增加一个Switch控键的想法。

Settings修改一:增加Switch控键_第1张图片


Settings修改一:增加Switch控键_第2张图片


跟下代码发现:

// http://blog.csdn.net/heymi_csdn 
       static int getHeaderType(Header header) {
            if (header.fragment == null && header.intent == null) {
                return HEADER_TYPE_CATEGORY;
            } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings
                     || header.id == R.id.hotknot_settings ) {
                return HEADER_TYPE_SWITCH;
            } else if (header.id == R.id.security_settings) {
                return HEADER_TYPE_BUTTON;
            } else {
                return HEADER_TYPE_NORMAL;
            }
        }

        于依葫芦画瓢,把选项的id  R.id.development_settings加进去,然后就是出现了Settings修改一:增加Switch控键_第3张图片


        但是这仅仅是能看而已,根本没有任何功能。然后往下找,发现了:

            // All view fields must be updated every time, because the view may be recycled
            switch (headerType) {
                case HEADER_TYPE_CATEGORY:
                    holder.title.setText(header.getTitle(getContext().getResources()));
                    break;

                case HEADER_TYPE_SWITCH:
                    // Would need a different treatment if the main menu had more switches
                    if (header.id == R.id.wifi_settings) {
                        mWifiEnabler.setSwitch(holder.switch_);
                    } else if (header.id == R.id.bluetooth_settings){
                        mBluetoothEnabler.setSwitch(holder.switch_);
                    } else if (header.id == R.id.hotknot_settings){
                        mHotKnotEnabler.setSwitch(holder.switch_);
                    }
                    updateCommonHeaderView(header, holder);
                    break;

        原来用是另一个类来设置和监控Switch的,然后再画瓢,写了个DevelopmentEnabler类,通过读写系统设置数据库Settings.Global.DEVELOPMENT_SETTINGS_ENABLED来设置Switch状态。

DevelopmentEnabler.java

package com.android.settings;

import android.content.Context;
import android.provider.Settings;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.provider.Settings;

// http://blog.csdn.net/heymi_csdn
public class DevelopmentEnabler implements
		CompoundButton.OnCheckedChangeListener {
	private final Context mContext;
	private Switch mSwitch;

	public DevelopmentEnabler(Context context, Switch mSwitch_) {
		mContext = context;
		mSwitch = mSwitch_;
	}

	public void setSwitch(Switch switch_) {
		if (mSwitch == switch_)
			return;
		mSwitch.setOnCheckedChangeListener(null);
		mSwitch = switch_;
		mSwitch.setOnCheckedChangeListener(this);

		int isDevelopment = Settings.Global.getInt(mContext.getContentResolver(),
				Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
		Log.v("heymi", "setSwitch isDevelopment = " + isDevelopment);
		if (isDevelopment == 0) {
			mSwitch.setChecked(false);
		} else {
			mSwitch.setChecked(true);
		}
	}
	public void setChecked(boolean isChecked){
		mSwitch.setChecked(isChecked);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			Settings.Global.putInt(mContext.getContentResolver(),
					Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
		} else {
			Settings.Global.putInt(mContext.getContentResolver(),
					Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
		}
		Log.v("heymi", "onCheckedChanged: isChecked = "+ isChecked);
	}

	public void resume() {
		int isDevelopment = Settings.Global.getInt(mContext.getContentResolver(),
				Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
		Log.v("heymi", "resume Development = " + isDevelopment);
		if (isDevelopment == 0) {
			mSwitch.setChecked(false);
		} else {
			mSwitch.setChecked(true);
		}
		mSwitch.setEnabled(true);
		mSwitch.setOnCheckedChangeListener(this);
		Log.v("heymi", "resume");
	}

	public void pause() {
		mSwitch.setEnabled(false);
		mSwitch.setOnCheckedChangeListener(null);
		Log.v("heymi", "pause");
	}
}

        然后两级菜单的switch控键状态就可以同步了。但是,当二级菜单没有这个控键,应该怎么同步呢。在BluetoothSettings中,发现在了这句:

mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);

        就是这么简单,把当前菜单的switch绑定到BluetoothEnabler中就行了。

又拿开发者选项来做实验。把DevelopmentSettings类中switch控键的操作全部都注释掉,按照上面的方式来绑定

mDevelopmentEnabler = new DevelopmentEnabler(activity,mEnabledSwitch);
在onstart()和onstop()中加入DevelopmentEnabler的resume()和pause()就行了。


注:

    本文代码是基于MTK 4.4.2系统的,跟安卓源码会有点差别。转载注明出处。


你可能感兴趣的:(Settings修改一:增加Switch控键)