system.setting-全局变量数据监听

今天在setting里添加了一个新的变量,想要实现对这个变量的监听。现在记录下方法

首先就是明白一点,我们在system.setting里添加的变量,都会被保存在data/data/com.android.providers.settings/databases/settings/system 里面

INSERT INTO "system" VALUES(120,'voice_unlock_screen',NULL);
INSERT INTO "system" VALUES(121,'voice_unlock_and_launch1',NULL);
INSERT INTO "system" VALUES(122,'voice_unlock_and_launch2',NULL);
INSERT INTO "system" VALUES(123,'voice_unlock_and_launch3',NULL);
INSERT INTO "system" VALUES(124,'volume_voice_earpiece','4');
INSERT INTO "system" VALUES(125,'volume_ring_speaker','8');
INSERT INTO "system" VALUES(126,'com_android_contacts_mtk_unread','0');
INSERT INTO "system" VALUES(127,'hide_rotation_lock_toggle_for_accessibility','0');
INSERT INTO "system" VALUES(133,'pointer_location','1');
INSERT INTO "system" VALUES(138,'nanlog_clock_style','0');

所以,如果我们要监听,我们只需要监听它所在的数据库就行了

1.写数据库监听动作

    private ContentObserver mAnalogClockObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            int value = Settings.System.getInt(
                    mContext.getContentResolver(), Settings.System.NANLOG_CLOCK_STYLE, DEFAULT_ANALOG_CLOCK);
            android.util.Log.e("zhangshuli", "value==="+value);
            updateClockView(value);
        }

    };

2.注册监听的数据库

        mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.NANLOG_CLOCK_STYLE),
                false, mAnalogClockObserver);

这里最主要的就是获得监听数据库的uri,如果我们打log就可以发现,uri如下

uri===content://settings/system/nanlog_clock_style

 

从system.setting获得变量数据库的方法就是getUriFor方法,具体实现如下

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/global");
getUriFor(CONTENT_URI, name);
        public static Uri getUriFor(Uri uri, String name) {
            return Uri.withAppendedPath(uri, name);
        } 

这样我们就可以实现数据库的监听了

posted @ 2015-07-14 14:22  小白屋  阅读(5333)  评论(0编辑  收藏  举报