Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.content.SharedPreferences;
     22 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
     23 import android.os.Bundle;
     24 import android.os.Handler;
     25 import android.preference.PreferenceActivity;
     26 import android.preference.CheckBoxPreference;
     27 import android.widget.Toast;
     28 
     29 /**
     30  * Example that shows finding a preference from the hierarchy and a custom preference type.
     31  */
     32 public class AdvancedPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
     33     public static final String KEY_MY_PREFERENCE = "my_preference";
     34     public static final String KEY_ADVANCED_CHECKBOX_PREFERENCE = "advanced_checkbox_preference";
     35 
     36     private CheckBoxPreference mCheckBoxPreference;
     37     private Handler mHandler = new Handler();
     38 
     39     /**
     40      * This is a simple example of controlling a preference from code.
     41      */
     42     private Runnable mForceCheckBoxRunnable = new Runnable() {
     43         public void run() {
     44             if (mCheckBoxPreference != null) {
     45                 mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
     46             }
     47 
     48             // Force toggle again in a second
     49             mHandler.postDelayed(this, 1000);
     50         }
     51     };
     52 
     53     @Override
     54     protected void onCreate(Bundle savedInstanceState) {
     55         super.onCreate(savedInstanceState);
     56 
     57         // Load the XML preferences file
     58         addPreferencesFromResource(R.xml.advanced_preferences);
     59 
     60         // Get a reference to the checkbox preference
     61         mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
     62                 KEY_ADVANCED_CHECKBOX_PREFERENCE);
     63     }
     64 
     65     @Override
     66     protected void onResume() {
     67         super.onResume();
     68 
     69         // Start the force toggle
     70         mForceCheckBoxRunnable.run();
     71 
     72         // Set up a listener whenever a key changes
     73         getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
     74     }
     75 
     76     @Override
     77     protected void onPause() {
     78         super.onPause();
     79 
     80         // Unregister the listener whenever a key changes
     81         getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
     82 
     83         mHandler.removeCallbacks(mForceCheckBoxRunnable);
     84     }
     85 
     86     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
     87         // Let's do something when my counter preference value changes
     88         if (key.equals(KEY_MY_PREFERENCE)) {
     89             Toast.makeText(this, "Thanks! You increased my count to "
     90                     + sharedPreferences.getInt(key, 0), Toast.LENGTH_SHORT).show();
     91         }
     92     }
     93 
     94 }
     95