Android的共享preference [英] Android-sharedpreference

查看:107
本文介绍了Android的共享preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜    我需要有关Android和preference数据共享preference

简单的解释
解决方案

据的共享preferences | Android开发教程(第13),由西Geetha MN 的,

  

许多应用可以提供一种方式来捕捉用户preferences   一个特定的应用程序或活动的设置。对于支持   这一点,Android提供了一个简单的API集。

     

preferences是典型的名称值对。它们可以被存储为   共享preferences在各种活动中的应用程序(注意   目前它不能跨进程共享)。或者它可以是   一些需要被存储到特定的活性。

  1. 共用preferences:共享preferences可以使用所有部件(活动,服务等)关闭应用

  2. 活动处理preferences:这些preferences只能与在活动中使用,不能使用的应用程序的其它部件

共享preferences:

共享preferences与的 getShared preferences 法上下文的帮忙,类。的preferences存储在默认文件(1)也可以指定(2)被用来指preferences一个文件名。

(1)这里是你如何获取实例时,您指定的文件名

 公共静态最后弦乐preF_FILE_NAME =prefFile;
   共享preferences preferences = getShared preferences(preF_FILE_NAME,MODE_PRIVATE);
 

MODE_PRIVATE 的操作模式为preferences。它是默认模式和装置所创建的文件将通过仅调用应用程序进行访问。支持另外两种模式是 MODE_WORLD_READABLE MODE_WORLD_WRITEABLE 。在 MODE_WORLD_READABLE 其他应用程序可以读取创建的文件,但不能修改它。如遇 MODE_WORLD_WRITEABLE 其他应用程序也有一个创建的文件的写权限。

(2)推荐的方法是默认的模式中使用,而不指定文件名

 共享preferences preferences = preferencesManager.getDefaultShared preferences(上下文);
 

最后,一旦你有preferences例如,这里是你如何能从preferences检索存储的值

 内部存储preference = preferences.getInt(storedInt,0);
 

存储值在preference文件共享preference.Editor 对象必须使用。 编辑共享preference 类的嵌套接口。

 共享preferences.Editor编辑器= preferences.edit();
editor.putInt(storedInt,存储preference); //值来存储
editor.commit();
 

编辑器还支持像方法删除()明确()删除从preference值该文件。

活动preferences:

共享preferences可以由其他应用程序组件。但是,如果你不需要共享preferences与其他组件,并希望有活动专用preferences。你可以做到这一点与获取preferences()活动的方法的帮助。该获取preference 方法使用 getShared preferences()方法与活动类的名称为preference文件名。

以下是code得到preferences

 共享preferences preferences = GET preferences(MODE_PRIVATE);
int的存储preference = preferences.getInt(storedInt,0);
 

在code储存的值也相同的情况下共享preferences的。

 共享preferences preferences = GET preference(MODE_PRIVATE);
共享preferences.Editor编辑器= preferences.edit();
editor.putInt(storedInt,存储preference); //值来存储
editor.commit();
 

您也可以使用其他的方法,如储存活动状态的数据库。注意的Andr​​oid还含有一种叫机器人。preference 包。该包定义类来实现应用程序preferences用户界面。

要看到更多的例子检查Android的数据存储开发商现场后。

Hi I need simple explanation about shared preference in android and preference data

解决方案

According to Shared Preferences | Android Developer Tutorial (Part 13) by Sai Geetha M N,

Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.

Preferences are typically name value pairs. They can be stored as "Shared Preferences" across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.

  1. Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.

  2. Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.

Shared Preferences:

The shared preferences are managed with the help of getSharedPreferences method of the Context class. The preferences are stored in a default file(1) or you can specify a file name(2) to be used to refer to the preferences.

(1) Here is how you get the instance when you specify the file name

public static final String PREF_FILE_NAME = "PrefFile";
   SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.

(2) The recommended way is to use by the default mode, without specifying the file name

SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences(context);

Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:

 int storedPreference = preferences.getInt("storedInt", 0);

To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

Editor also support methods like remove() and clear() to delete the preference value from the file.

Activity Preferences:

The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.

Following is the code to get preferences

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);

The code to store values is also same as in case of shared preferences.

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.

To see some more examples check Android's Data Storage post on developers site.

这篇关于Android的共享preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆