Code example for JSONObject

Methods: getBoolean, getString, has, isNull

0
     * branch name, or null for the default branch. 
     * message.enable should be a boolean: true to enable listening, false to 
     * disable listening. 
     */ 
    private void handleObserve(JSONObject message) throws JSONException {
        if (!message.has("branch")) {
            Log.e(LOGTAG, "No branch specified for SharedPreference:Observe; aborting.");
            return; 
        } 
 
        String branch = message.isNull("branch") ? null : message.getString("branch");
        SharedPreferences prefs = getSharedPreferences(branch);
        boolean enable = message.getBoolean("enable");
 
        // mListeners is only modified in this one observer, which is called 
        // from Gecko serially. 
        if (enable && !this.mListeners.containsKey(branch)) {
            SharedPreferences.OnSharedPreferenceChangeListener listener = new ChangeListener(branch);
            this.mListeners.put(branch, listener);
            prefs.registerOnSharedPreferenceChangeListener(listener);
        }