1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.example.android.networkusage;
16 
17 import android.content.SharedPreferences;
18 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
19 import android.os.Bundle;
20 import android.preference.PreferenceActivity;
21 import com.example.android.networkusage.R;
22 
23 /**
24  * This preference activity has in its manifest declaration an intent filter for
25  * the ACTION_MANAGE_NETWORK_USAGE action. This activity provides a settings UI
26  * for users to specify network settings to control data usage.
27  */
28 public class SettingsActivity extends PreferenceActivity
29         implements
30             OnSharedPreferenceChangeListener {
31 
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35 
36         // Loads the XML preferences file.
37         addPreferencesFromResource(R.xml.preferences);
38     }
39 
40     @Override
onResume()41     protected void onResume() {
42         super.onResume();
43 
44         // Registers a callback to be invoked whenever a user changes a preference.
45         getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
46     }
47 
48     @Override
onPause()49     protected void onPause() {
50         super.onPause();
51 
52         // Unregisters the listener set in onResume().
53         // It's best practice to unregister listeners when your app isn't using them to cut down on
54         // unnecessary system overhead. You do this in onPause().
55         getPreferenceScreen()
56                 .getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
57     }
58 
59     // Fires when the user changes a preference.
60     @Override
onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)61     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
62         // Sets refreshDisplay to true so that when the user returns to the main
63         // activity, the display refreshes to reflect the new settings.
64         NetworkActivity.refreshDisplay = true;
65     }
66 }
67