前段时间做个了个简单的菜单,在setting 里面做一个功能开关,这个开关的值需要在framework和其他应用中用到。。这里主要涉及到数据的共享,
考虑到这个问题,查了些资料,最后决定用Settings.System.getInt直接存储。关于Settings.System.putInt,是存在android系统设置的数据库里,这个方
法是android进行封装了一层的。数据库的路径就是:/data/data/com.android.providers.settings.
相关测试代码:
package com.example.setpreference;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.provider.Settings;
import android.util.Log;
public class MainActivity extends PreferenceActivity implements OnPreferenceChangeListener{
private CheckBoxPreference mGesture;
private CheckBoxPreference mCall;
private CheckBoxPreference mBrowse;
private static final String KEY_GESTURE = "key_gesture";
private static final String KEY_CALL = "key_call_gesture";
private static final String KEY_BROWSE = "key_browse_gesture";
private static final String GESTURE_ON = "gesture.setting.on";
private static final String GESTURE_CALL_ON = "gesture.call_setting.on";
private static final String GESTURE_BROWSE_ON = "gesture.browse.setting.on";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.gesture_setting);
mGesture = (CheckBoxPreference)findPreference(KEY_GESTURE);
mCall = (CheckBoxPreference)findPreference(KEY_CALL);
mBrowse = (CheckBoxPreference)findPreference(KEY_BROWSE);
mGesture.setOnPreferenceChangeListener(this);
mCall.setOnPreferenceChangeListener(this);
mBrowse.setOnPreferenceChangeListener(this);
initCheckeState();
}
private void initCheckeState(){
mGesture.setChecked(Settings.System.getInt(getContentResolver(), GESTURE_ON, 0) == 1);
mCall.setChecked(Settings.System.getInt(getContentResolver(), GESTURE_CALL_ON, 0) == 1);
mBrowse.setChecked(Settings.System.getInt(getContentResolver(), GESTURE_BROWSE_ON, 0) == 1);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
int value = 0;
if((Boolean)newValue){
value = 1;
}
String preferenceKey = preference.getKey();
if(preferenceKey.equals(KEY_GESTURE)){
Settings.System.putInt(getContentResolver(), GESTURE_ON, value);
}else if(preferenceKey.equals(KEY_CALL)){
Settings.System.putInt(getContentResolver(), GESTURE_CALL_ON, value);
}else if(preferenceKey.equals(KEY_BROWSE)){
Settings.System.putInt(getContentResolver(), GESTURE_BROWSE_ON, value);
}
return true;
}
}
************************************
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/air_gestures">
<CheckBoxPreference
android:key="key_gesture"
android:title="@string/air_gestures"
android:summary="@string/air_gestures"
android:summaryOn="@string/air_gestures_on"
android:summaryOff="@string/air_gestures_off"
android:persistent="false"
/>
<CheckBoxPreference
android:key="key_browse_gesture"
android:title="@string/air_gestures_browse_title"
android:summary="@string/air_gestures_browse_summary"
android:dependency="key_gesture"
android:persistent="false"
/>
<CheckBoxPreference
android:key="key_call_gesture"
android:title="@string/air_gestures_call_title"
android:summary="@string/air_gestures_call_summary"
android:dependency="key_gesture"
android:persistent="false"
/>
</PreferenceScreen>
**************
需加权限
<uses-permission android:name="android.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
最后 补充 ,如果需要监听数据变化可用
registerContentObserver( Settings.System.getUriFor(String ), true,ContentObserver)