Android的共享preferences最佳实践 [英] Android SharedPreferences Best Practices

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

问题描述

在一个应用程序,我一直在建设我们依靠共享preferences了不少,这让我思考什么是最好的做法,当谈到访问共享preferences。比如很多人说合适的方式来访问它是通过此调用:

  preferenceManager.getDefaultShared preferences(上下文的背景下)
 

不过看起来这可能是危险的。如果您有依赖于共享preferences你可以有钥匙的重复,特别是在使用依赖于共享preferences以及一些第三方库的情况下,大量的应用程序。在我看来,这将更好的通话使用将是:

  Context.getShared preferences(字符串名称,诠释模式)
 

这样,如果你有在很大程度上依赖于共享preferences可以创建仅由你的类preference文件的类。您可以使用类的全名,以确保文件将最有可能不会被别人复制。

也是基于这样的SO问题:<一href="http://stackoverflow.com/questions/4371273/should-accessing-shared$p$pferences-be-done-off-the-ui-thread">Should访问共享preferences进行脱UI线程?中,似乎访问共享preferences应做关闭UI线程这是有意义的。

是否有Android开发者应在其应用程序中使用的共享preferences时要充分考虑?任何其他最佳做法

解决方案
  

如果您有依赖于共享preferences你可以有钥匙的重复,特别是在使用依赖于共享preferences以及一些第三方库的情况下,大量的应用程序。

库不应当使用该特定共享preferences 。默认共享preferences 只能由应用程序使用。

  

这样,如果你有在很大程度上依赖于共享preferences一类,你可以创建一个只使用你的类preference文件。

您当然欢迎这样做。我不会,在应用层面上,作为主要的原因共享preferences 是让他们在应用程序组件之间共享。开发团队应该没有问题,管理这个命名空间,就像他们应该没有问题,管理类,包,资源,或其他项目级的东西的名称。此外,默认共享preferences 是你的 preferenceActivity 将使用。

不过,要回你的库点,可重复使用的<​​/ em>的库应该使用单独的共享preferences 的只有自己的图书馆。我不会基于它的类名,因为你是一体的重构远离破坏你的应用程序。相反,选择一个名字是唯一的(例如,基于库的名称,如com.commonsware.cwac.wakeful.WakefulIntentService),但稳定的。

  

似乎访问共享preferences应该做关闭UI线程这是有道理的。

在理想情况下,是的。我最近发布了 共享preferencesLoader 与此帮助

  

是否有Android开发者应在其应用程序中使用的共享preferences时要充分考虑?任何其他最佳做法

不要过度依赖于他们。它们被存储在XML文件和不是事务。数据库应该是你的主要数据存储,特别是对于数据你真的不想失去。

In an application I have been building we rely on SharedPreferences quite a bit, this got me thinking about what is best practice when it comes to accessing SharedPreferences. For instance many people say the appropriate way to access it is via this call:

PreferenceManager.getDefaultSharedPreferences(Context context)

However it seems like this could be dangerous. If you have a large application that is relying on SharedPreferences you could have key duplication, especially in the case of using some third party library that relies on SharedPreferences as well. It seems to me that the better call to use would be:

Context.getSharedPreferences(String name, int mode)

This way if you have a class that heavily relies on SharedPreferences you can create a preference file that is used only by your class. You could use the fully qualified name of the class to ensure that the file will most likely not be duplicated by someone else.

Also based on this SO question: Should accessing SharedPreferences be done off the UI Thread?, it seems that accesses SharedPreferences should be done off the UI thread which makes sense.

Are there any other best practices Android developers should be aware of when using SharedPreferences in their applications?

解决方案

If you have a large application that is relying on SharedPreferences you could have key duplication, especially in the case of using some third party library that relies on SharedPreferences as well.

Libraries should not use that particular SharedPreferences. The default SharedPreferences should only be used by the application.

This way if you have a class that heavily relies on SharedPreferences you can create a preference file that is used only by your class.

You are certainly welcome to do this. I wouldn't, at the application level, as the primary reason for SharedPreferences is to have them be shared among the components in the application. A development team should have no problem managing this namespace, just as they should have no problem managing names of classes, packages, resources, or other project-level stuff. Moreover, the default SharedPreferences are what your PreferenceActivity will use.

However, going back to your libraries point, reusable libraries should use a separate SharedPreferences for their library only. I would not base it on a class name, because then you are one refactoring away from breaking your app. Instead, pick a name that is unique (e.g., based on the library name, such as "com.commonsware.cwac.wakeful.WakefulIntentService") but stable.

it seems that accesses SharedPreferences should be done off the UI thread which makes sense.

Ideally, yes. I recently released a SharedPreferencesLoader that helps with this.

Are there any other best practices Android developers should be aware of when using SharedPreferences in their applications?

Don't over-rely upon them. They are stored in XML files and are not transactional. A database should be your primary data store, particularly for data you really don't want to lose.

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

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