我如何从一个preferenceActivity在Android上的共享preferences?preferenceActivity、Android、preferences

2023-09-11 11:07:05 作者:要么爱要么滚﹏

我使用的是preferenceActivity,以示对我的应用程序的某些设置。我膨胀通过一个XML文件中的设置,使我的onCreate(和完整的类方法)看起来是这样的:

 公共类FooActivity扩展preferenceActivity {
    @覆盖
    公共无效的onCreate(包冰柱){
        super.onCreate(冰柱);
        加preferencesFromResource(R.xml preference。);
    }
}
 

的 preferenceActivity $p$pferenceFragment指出,

  

这些preferences会自动保存到共享preferences作为用户与他们互动。要检索的,在本次活动的preference阶层将使用共享preferences的实例,调用getDefaultShared preferences(android.content.Context),在同一个包这一活动的背景下。

但我怎么得到的共享preference在另一个活动的名称?我只能叫

  getShared preferences(名称,模式)
 
Android之PreferenceActivity 详解

在其他活动,但我需要这是使用的preferenceActivity共享preference的名称。叫什么名字或者我该如何找回?

解决方案

 进口安卓preference preferenceManager。;
共享preferences preFS = preferenceManager.getDefaultShared preferences(本);
//然后使用
prefs.getBoolean(KeyString中,真正的);
 

更新

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

  

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

     

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

共用preferences:共享preferences可以使用所有部件的应用程序(活动,服务等)

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

共享preferences:

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

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

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

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

 公共静态最后弦乐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 其他应用程序也有一个创建的文件的写权限。

最后,一旦你有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的数据存储后开发商的网站。

I am using a PreferenceActivity to show some settings for my application. I am inflating the settings via a xml file so that my onCreate (and complete class methods) looks like this:

public class FooActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.preference);
    }
}

The javadoc of PreferenceActivity PreferenceFragment states that

These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android.content.Context) with a context in the same package as this activity.

But how I get the name of the SharedPreference in another Activity? I can only call

getSharedPreferences(name, mode)

in the other activity but I need the name of the SharedPreference which was used by the PreferenceActivity. What is the name or how can i retrieve it?

解决方案

import android.preference.PreferenceManager;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// then you use
prefs.getBoolean("keystring", true);

Update

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.

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

Activity handled preferences: These preferences can only be used within the particular 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) The recommended way is to use by the default mode, without specifying the file name

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

(2) 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 modes 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.

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 a nested interface in the SharedPreference class.

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

Editor also supports methods like remove() and clear() to delete the preference values 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 activity 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 the 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.