Setup the Preferences

Android preferences is a key/value entries that store data that can be specific to a certain activity or shared among all activities within the application. 

the data are stored in a xml file.

  1. Preferences can be retrieved only by a single activity, we do it like this: we get a SharedPreferences object by calling getPreferences(int mode)

SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);

SharedPreferences.Editor editor=prefs.edit(); editor.putString(“pref 1”, “some text”); editor.commit();

  • the mode value can be one of the following:
      1. Context.MODE_PRIVATE (0): a file creating mode that makes the created file only accessible by applications with the same user ID (access the file from the same application context, will desctribe later).
      2. Context.MODE_WORLD_READABLE (1): file mode makes the file readable from other applications.
      3. Context.MODE_WORLD_WRITEABLE (2): file mode allows other applications to write to the file.

we get an instance of SharedPreferences.Editor and write the preference value with editor.putString(String key, String value) method.
shared preferences allows you to insert preferences using the following methods:

  1. editor.putBoolean(String key, boolean value).
  2. editor.putFloat(String key,float value).
  3. editor.putInt(String key, int value).
  4. editor.putLong(String key, long value)
  5. editor.putString(String key, String value)

then we call edit.commit() to save the preferences to the file. commit returns a boolean indicating the result of saving, true if successful and false if failed.

It can be shared and retrieved among all activities within the application, or through all applications on the device. To save preferences that can be retrieved from all activities in the application we do it like this:

SharedPreferences prefs= getSharedPreferences("demopref", Context.MODE_WORLD_READABLE);
        SharedPreferences.Editor editor=prefs.edit();
        editor.putString("demostring", "hello");
        editor.commit();

we give our preferences file a name (demopref in this case) so that other activities can reference that preferences file.
Sharing preferences across applications: 

We can store preferences in one application and read them in another application, this is done reading the preferences file by loading them through the first application’s context. let’s assume we have two applications:

  1. Application 1 with package name “com.mina.prefdemo“.
  2. Application2 with package name “com.mina.demoapp“.
If application1 creates a preferences file with the name “demopref” and inserts a String preference with the key/value “demostring/hello”.
now we access this file and value from application 2 like this:
Context con;
  try {
   con = createPackageContext("com.minasamy.prefdemo", 0);
   SharedPreferences pref=con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
   String x=pref.getString("demostring", "not found");
   txt.setText(x);
  } catch (NameNotFoundException e) {
   Log.e(Tag, e.toString());
  }

// Setup the Preferences for this application, by class.
    Preferences prefs = Preferences.userNodeForPackage(MainClass.class);

   // Retrieve some preferences previously stored, with defaults in case this is the first run.
    String text = prefs.get(“A”“a”);
    String display = prefs.get(“B”“b”);

// Assume the user chose new preference values: Store them back.
    prefs.put(“A”“aa”);
    prefs.put(“B”“bb”);

 

Advertisements
生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web