在活动和服务之间共享数据 [英] Sharing data amongst activities and services

查看:85
本文介绍了在活动和服务之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小型Android项目,需要在多个活动之间共享一些数据,并在一个单独的进程中运行一个服务。我只想知道在共享数据方面我的选择是什么?应用类? IPC?基于文件的?广播?
谢谢你们!

I am working on a small android project where it is necessary to share some data amongst several activities and a service that runs in a separate process. I would just like to know what are my options in terms of sharing data? Application class? IPC? File-based? Broadcasts? Thanks guys!

推荐答案

1。听起来你需要广播一些信息。您可以在任何想要获得通知的活动/服务中设置广播接收者。

1 . Sounds like you need to broadcast some information. You than will be able to set broadcast receivers in any activity/service you would like to get notified.

在线阅读有关 Broadcastreceiver 和关于发送广播

2 。
如何在单个应用程序中的活动/服务之间传递数据?

这取决于您想要的数据类型分享:

It depends on the type of data that you want to share:

原始数据类型
要在应用程序中的活动/服务之间共享原始数据,请使用Intent.putExtras() 。为了传递需要持久使用首选项存储机制的原始数据。

Primitive Data Types To share primitive data between Activities/Services in an application, use Intent.putExtras(). For passing primitive data that needs to persist use the Preferences storage mechanism.

非持久性对象
为了在短时间内共享复杂的非持久性用户定义的对象,建议采用以下方法:

Non-Persistent Objects For sharing complex non-persistent user-defined objects for short duration, the following approaches are recommended:

android.app.Application类

android.app.Application是需要维护全局应用程序状态的人的基础类。可以通过任何活动或服务的getApplication()访​​问它。它有几种生命周期方法,如果您在AndroidManifest.xml中注册,将自动实例化Android。

The android.app.Application is a base class for those who need to maintain global application state. It can be accessed via getApplication() from any Activity or Service. It has a couple of life-cycle methods and will be instantiated by Android automatically if your register it in AndroidManifest.xml.

公共静态字段/方法

通过活动/服务访问数据的另一种方法是使用公共静态字段和/或方法。您可以从应用程序中的任何其他类访问这些静态字段。要共享对象,创建对象的活动将设置一个静态字段来指向该对象,并且任何其他想要使用此对象的活动只是访问此静态字段。

An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.

对对象的弱引用的HashMap

您还可以使用具有长键的对象的WeakReferences的HashMap。当活动想要将对象传递给另一个活动时,它只需将对象放在地图中,并通过意图追加将密钥(基于计数器或时间戳的独特Long)发送给收件人活动。收件人活动使用此密钥检索对象。

You can also use a HashMap of WeakReferences to Objects with Long keys. When an activity wants to pass an object to another activity, it simply puts the object in the map and sends the key (which is a unique Long based on a counter or time stamp) to the recipient activity via intent extras. The recipient activity retrieves the object using this key.

单身人士类

使用静态Singleton有很多优点,比如你可以引用它们,而不是将getApplication()转换为特定于应用程序的类,或者在所有应用程序子类中挂起接口的麻烦,以便您的各种模块可以参考那个界面代替了。

There are advantages to using a static Singleton, such as you can refer to them without casting getApplication() to an application-specific class, or going to the trouble of hanging an interface on all your Application subclasses so that your various modules can refer to that interface instead.

但是,静态的生命周期并不在你的控制之下;所以为了遵守生命周期模型,应用程序类应该在应用程序类的onCreate()和onTerminate()方法中启动和拆除这些静态对象。

But, the life cycle of a static is not well under your control; so to abide by the life-cycle model, the application class should initiate and tear down these static objects in the onCreate() and onTerminate() methods of the Application Class

持久对象
即使应用程序似乎继续运行,系统可能会选择终止其进程并稍后重新启动。如果您需要从一个活动调用持续到下一个活动的数据,则需要将该数据表示为在被通知可能会消失时被活动保存的状态。

Persistent Objects Even while an application appears to continue running, the system may choose to kill its process and restart it later. If you have data that you need to persist from one activity invocation to the next, you need to represent that data as state that gets saved by an activity when it is informed that it might go away.

为了共享复杂的持久性用户定义对象,建议采用以下方法:

For sharing complex persistent user-defined objects, the following approaches are recommended:

Application Preferences
Files
contentProviders
SQLite DB

如果共享数据需要保留跨越可以杀死应用程序进程的点,然后将数据放置在持久存储器中,如应用程序首选项,SQLite DB,文件或ContentProviders。请参阅数据存储进一步有关如何使用这些组件的详细信息。

If the shared data needs to be retained across points where the application process can be killed, then place that data in persistent storage like Application Preferences, SQLite DB, Files or ContentProviders. Please refer to the Data Storage for further details on how to use these components.

这篇关于在活动和服务之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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