Settings修改一:增加Switch控键
2015-11-02 10:19
507 查看
系统设置一级菜单上wifi和蓝牙有个Switch按键,而且它和二级菜单上的Switch是同步的。而开发者选项菜单只是在二级菜单上有Switch控键,一级没有,于是产生了在一级菜单上增加一个Switch控键的想法。
跟下代码发现:
于依葫芦画瓢,把选项的id R.id.development_settings加进去,然后就是出现了
但是这仅仅是能看而已,根本没有任何功能。然后往下找,发现了:
原来用是另一个类来设置和监控Switch的,然后再画瓢,写了个DevelopmentEnabler类,通过读写系统设置数据库Settings.Global.DEVELOPMENT_SETTINGS_ENABLED来设置Switch状态。
DevelopmentEnabler.java
然后两级菜单的switch控键状态就可以同步了。但是,当二级菜单没有这个控键,应该怎么同步呢。在BluetoothSettings中,发现在了这句:
就是这么简单,把当前菜单的switch绑定到BluetoothEnabler中就行了。
又拿开发者选项来做实验。把DevelopmentSettings类中switch控键的操作全部都注释掉,按照上面的方式来绑定
注:
本文代码是基于MTK 4.4.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加进去,然后就是出现了
但是这仅仅是能看而已,根本没有任何功能。然后往下找,发现了:
// 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系统的,跟安卓源码会有点差别。转载注明出处。
相关文章推荐
- PHP8新特性之match表达式
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件