android 監聽系統屬性變化的方法

阅读更多

 

關鍵代碼如下:

class SettingsObserver extends ContentObserver {
       SettingsObserver(Handler handler) {
            super(handler);
        }
        void observe() {
            // Observe all users' changes
            ContentResolver resolver = mSystemContext.getContentResolver();
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.KEY_TARGET_DIRECTORY), true, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.KEY_ENABLE_AUTO_SYSTEM), true, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.KEY_UPDATE_INTERVAL), true, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.KEY_CHECK_TIME), true, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.KEY_CHECK_TIME_VALUE), true, this,
                    UserHandle.USER_ALL);
        }

        @Override public void onChange(boolean selfChange) {
            try {
                checkTime = Settings.System.getString(mSystemContext.getContentResolver(),Settings.System.KEY_CHECK_TIME); 
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

 

 

你可能感兴趣的:(android,frameworks)