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

本文是小编为大家收集整理的关于在各种活动和服务之间共享数据的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在开发一个小型 android 项目,在该项目中,有必要在多个活动和在单独进程中运行的服务之间共享一些数据.我只想知道在共享数据方面我有哪些选择?应用类?工控机?基于文件?广播?谢谢大家!

推荐答案

1 .听起来你需要广播一些信息.您将能够在您希望收到通知的任何活动/服务中设置广播接收器.

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

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

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

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

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

android.app.Application 类

android.app.Application 是需要维护全局应用程序状态的基类.可以从任何 Activity 或 Service 通过 getApplication() 访问它.它有几个生命周期方法,如果你在 AndroidManifest.xml 中注册它会被 Android 自动实例化.

公共静态字段/方法

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

对象弱引用的 HashMap

您还可以将 WeakReferences 的 HashMap 用于具有 Long 键的对象.当一个 Activity 想要将一个对象传递给另一个 Activity 时,它只需将该对象放入映射中并通过 Intent Extras 将密钥(这是一个基于计数器或时间戳的唯一 Long)发送给接收者 Activity.接收者活动使用此键检索对象.

单例类

使用静态 Singleton 有很多好处,例如您可以引用它们而无需将 getApplication() 强制转换为特定于应用程序的类,或者麻烦地在所有应用程序子类上挂一个接口,以便您的各种模块可以参考那个接口.

但是,静态的生命周期并不能完全由您控制;所以为了遵守生命周期模型,应用程序类应该在应用程序类的 onCreate() 和 onTerminate() 方法中初始化和拆除这些静态对象

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

对于共享复杂的持久性用户定义对象,推荐以下方法:

Application Preferences
Files
contentProviders
SQLite DB

如果需要跨应用程序进程可能被终止的点保留共享数据,则将该数据放置在持久存储中,如应用程序首选项、SQLite DB、文件或内容提供程序.请参考数据存储有关如何使用这些组件的详细信息.

本文地址:https://www.itbaoku.cn/post/627623.html

问题描述

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 . 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.

Read more online about Broadcastreceiver and about send broadcast

2 . How do I pass data between Activities/Services within a single application?

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

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:

The android.app.Application class

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.

A public static field/method

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.

A HashMap of WeakReferences to Objects

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.

A Singleton class

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.

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

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.