访问其他应用程序的SharePreferences

      SharedPreferences是一种轻量级的数据存储方式,它可以用键值对的方式把简单数据类型(booleanintfloatlongString)存储在应用程序的私有目录下(data/data/包名/shared_prefs/)自己定义的xml文件中。

     

以下表格为获取SharedPreferences对象的两个方法:

返回值

函数

备注

SharedPreferences

Context.getSharedPreferences(String name,int mode)

name为本组件的配置文件名(如果想要与本应用程序的其他组件共享此配置文件,可以用这个名字来检索到这个配置文件)。

mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。

SharedPreferences

Activity.getPreferences(int mode)

配置文件仅可以被调用的Activity使用。

mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。

如果要读取配置文件信息,只需要直接使用SharedPreferences对象的getXXX()方法即可,而如果要写入配置信息,则必须先调用SharedPreferences对象的edit()方法,使其处于可编辑状态,然后再调用putXXX()方法写入配置信息,最后调用commit()方法提交更改后的配置文件。

Saving Preferences

We can save preferences in three ways:

  1. Preferences can be retrieved only by a single activity.
  2. Preferences can be shared and retrieved among all activities within the application.
  3. Preferences can be shared and retrieved through all applications on the device.

Saving Activity-level preferences:

To save preferences that are accessed only from a single activity, we do it like this:

SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=prefs.edit();
        editor.putString("pref 1", "some text");

        editor.commit();

We get a SharedPreferences object by calling getPreferences(int mode) method which takes an integer value as a parameter, 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.

Then 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.

Reading preferences values:

To read preferences values:

SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
String val=prefs.getString("pref 1", "some text");

We use sharedpreferences.getString(String key, String defaultValue) (or get boolean/float/int) to return the value stored with a specific key or defaultValue if not found.

Saving Application-level preferences:

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();

Same as the code above, but the difference is that 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());
  }

      在一个应用程序里,需要访问另外一个应用程序的 SharePreferences,在我们的应用程序里可以这样进行:

      Context context=null;

        try {
                context=createPackageContext("cs2c.AL",Context.CONTEXT_IGNORE_SECURITY);//其中"cs2c.AL"就是我们访问的应用程序的包名,获取这个包名的context
        } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

        }

       

        SharedPreferences settings=context.getSharedPreferences("musicEQ", 3);//3表示是即可读又可写,即MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE的值

       boolean  isSystemWallperSettings=settings.getBoolean("musicEQ", false);//获取另外一个应用程序的SharedPreferences的值。


你可能感兴趣的:(String,File,Integer,application,Access,reference)