共享数据之间的活动和服务和服务、数据

由网友(Tenderness°冬夏莫凉)分享简介:我工作的一个小机器人项目中,有必要分享一些数据之间的几个活动,并运行在一个单独的进程的服务。我只是想知道什么是我在分享数据方面的选择吗?应用程序类? IPC?基于文件的?广播?谢谢你们!I am working on a small android project where it is necessary to...

我工作的一个小机器人项目中,有必要分享一些数据之间的几个活动,并运行在一个单独的进程的服务。我只是想知道什么是我在分享数据方面的选择吗?应用程序类? 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而关于send广播

Read more online about Broadcastreceiver and about send broadcast

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

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

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

基本数据类型 要在应用程序中共享活动/服务的原始数据,使用Intent.putExtras()。传递需要坚持使用$p$pferences存储机制

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()的任何活动或服务的访问。它有一对夫妇的生命周期方法,将搭载Android自动初始化,如果您在AndroidManifest.xml中注册。

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.

在WeakReferences的一个HashMap到对象

您也可以使用在WeakReferences的一个HashMap与长键的对象。当一个活动想将对象传递到另一个活动,它简单地把该对象中的地图,并发送经由意图额外的键(这是一个独特的龙基于计数器或时间戳)给收件人活性。收件人活动获取使用此键的对象。

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

持久化对象 即使在一个应用程序似乎继续运行,该系统可以选择杀其过程和后来重新启动它。如果您有需要坚持从一个活动调用到下一个数据,你需要重新present,这一被保存的一个活动时,被告知数据的状态,它可能会消失。

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

如果需要共享数据跨越点,其中应用程序可以杀死被保留,然后将在持久性存储如应用preferences时,SQLite数据库,文件或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.

阅读全文

相关推荐

最新文章