我可以使用android.preference包执行数据库特定的操作吗? [英] Can I perform database specific operation using android.preference package?

查看:273
本文介绍了我可以使用android.preference包执行数据库特定的操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个数据库,其中我可以存储数据和获取数据,每当我需要。
这是可能与android.preference包。
我不想使用sqlite数据库



关于

解决方案

根据共享首选项| Android开发人员教程(第13部分) by Sai Geetha MN,


许多应用程序可能提供一种方法来捕获用户对
特定应用程序或活动的设置。为了支持
,Android提供了一组简单的API。



首选项通常是名称值对。它们可以作为
存储为应用程序中的各种活动的共享首选项(注意
目前无法在进程间共享)。或者它可以是
需要存储特定于活动的东西。





  1. p>共享首选项:共享首选项可以由应用程序的所有组件(活动,服务等)使用。


  2. 活动处理的首选项:


共享偏好设定:



共用偏好设定是透过 getSharedPreferences $ c> Context 类。首选项存储在默认文件(1)中,或者您可以指定用于引用首选项的文件名(2)。



指定文件名时如何获取实例

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

MODE_PRIVATE 是偏好设定的操作模式。它是默认模式,意味着只有调用应用程序将访问创建的文件。支持的其他两种模式是 MODE_WORLD_READABLE MODE_WORLD_WRITEABLE 。在 MODE_WORLD_READABLE 其他应用程序可以读取创建的文件,但不能修改它。在 MODE_WORLD_WRITEABLE 的情况下,其他应用程序也有创建文件的写入权限。



(2)推荐方式是在默认模式下使用而不指定文件名

  SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences上下文); 

最后,一旦您有偏好设置实例,以下是如何检索存储的值

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

在偏好文件中存储值 SharedPreference .Editor 对象。 编辑器 SharedPreference 类的嵌套界面。

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

编辑器还支持 remove()



活动偏好设定 clear() >



共享首选项可由其他应用程序组件使用。但如果你不需要与其他组件共享首选项,并希望有活动的私人偏好。你可以在活动的 getPreferences()方法的帮助下做到这一点。 getPreference 方法使用具有偏好设置文件名的活动类名的 getSharedPreferences()方法。 p>

以下是获取首选项的代码

  SharedPreferences首选项= getPreferences(MODE_PRIVATE ); 
int storedPreference = preferences.getInt(storedInt,0);

存储值的代码也与共享首选项的情况相同。

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

您还可以使用其他方法,如在数据库中存储活动状态。注意Android还包含一个名为 android.preference 的包。该包定义了实现应用程序首选项UI的类。



要查看更多示例,请检查Android的数据存储发布在开发者网站上。


I need a database in which I can store data and get data whenever I need. Is this possible with android.preference package. I do not want to use sqlite database

regards

解决方案

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天全站免登陆