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.preference; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.content.Intent; 23 import android.content.SharedPreferences; 24 import android.os.Bundle; 25 import android.preference.PreferenceManager; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.widget.Button; 29 import android.widget.LinearLayout; 30 import android.widget.TextView; 31 import android.widget.LinearLayout.LayoutParams; 32 33 /** 34 * Demonstrates launching a PreferenceActivity and grabbing a value it saved. 35 */ 36 public class LaunchingPreferences extends Activity implements OnClickListener { 37 38 private static final int REQUEST_CODE_PREFERENCES = 1; 39 40 private TextView mCounterText; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 46 /* 47 * These preferences have defaults, so before using them go apply those 48 * defaults. This will only execute once -- when the defaults are applied 49 * a boolean preference is set so they will not be applied again. 50 */ 51 PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false); 52 53 // Simple layout 54 LinearLayout layout = new LinearLayout(this); 55 layout.setOrientation(LinearLayout.VERTICAL); 56 setContentView(layout); 57 58 // Create a simple button that will launch the preferences 59 Button launchPreferences = new Button(this); 60 launchPreferences.setText(getString(R.string.launch_preference_activity)); 61 launchPreferences.setOnClickListener(this); 62 layout.addView(launchPreferences, new LayoutParams(LayoutParams.MATCH_PARENT, 63 LayoutParams.WRAP_CONTENT)); 64 65 mCounterText = new TextView(this); 66 layout.addView(mCounterText, new LayoutParams(LayoutParams.MATCH_PARENT, 67 LayoutParams.WRAP_CONTENT)); 68 69 updateCounterText(); 70 } 71 72 public void onClick(View v) { 73 74 // When the button is clicked, launch an activity through this intent 75 Intent launchPreferencesIntent = new Intent().setClass(this, AdvancedPreferences.class); 76 77 // Make it a subactivity so we know when it returns 78 startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES); 79 } 80 81 @Override 82 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 83 super.onActivityResult(requestCode, resultCode, data); 84 85 // The preferences returned if the request code is what we had given 86 // earlier in startSubActivity 87 if (requestCode == REQUEST_CODE_PREFERENCES) { 88 // Read a sample value they have set 89 updateCounterText(); 90 } 91 } 92 93 private void updateCounterText() { 94 // Since we're in the same package, we can use this context to get 95 // the default shared preferences 96 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); 97 final int counter = sharedPref.getInt(AdvancedPreferences.KEY_MY_PREFERENCE, 0); 98 mCounterText.setText(getString(R.string.counter_value_is) + " " + counter); 99 } 100 } 101