广播、内容提供商和内容接收器的最佳实践

发布于 2024-12-07 08:42:38 字数 486 浏览 0 评论 0原文

好的,首先我要说的是我是 Android 新手。我做了一些小项目,玩了一些东西,还阅读了大量的书。这就是我想要完成的任务:

我有一个在手机前台运行的远程服务,可以通过 WiFi 网络与其他 Android 设备发送和接收信息。手机上为使用此服务而构建的 APK 包含多个 SQLite DB。我希望这些 APK 向服务注册内容提供者,以便服务始终知道将其收到的数据放在哪里(我已经完成了这项任务)。当数据进来时,我将确定它所属的位置并将其放置在正确的数据库中(这里没有问题)。那么通知正确的活动已收到新数据的最佳方式是什么。我是否需要注册广播接收器以供我的服务调用?如果 APK 被操作系统杀死,广播接收器还会工作吗?加载正确的活动后,我是否会使用内容观察器来显示新数据?为什么我要使用 IntentServices?

我很抱歉问了这么多问题。就像我说的,我学得很快,但 Android 的 SKD 非常庞大,而且有很多很酷的东西。我想确保我通过最佳实践正确地思考问题,并使之非常容易维护和扩展。感谢我能得到的任何帮助或建议。

Okay so up front I will say that I am new to Android. I have made a few small projects and played with a few things and done tons and tons of reading. So here is the task I am trying to accomplish:

I have a remote service that runs in the foreground of my phone that can send and receive information with other android devices over a wifi network. APK's on the phone that are built to use this service contain multiple SQLite DB's. I want these APK's to register there content providers with the service so the service always know where to put data that it recieves (I have accomplished this task). When data comes in I will identify where it belongs and place it in the right DB (no problem here). So what would be the best way to notify the correct activity that new data has been received. Do I register Broadcast Receivers for my service to call? Will Broadcast Receivers work if the APK has been killed by the OS? Once the correct activity is loaded would I use a content observer to show the new data? Why would I use IntentServices?

I'm sorry for so many questions. Like I said I am learning fast but Android's SKD is massive and there are so many cool things. I want to make sure I am thinking things through right with best practices and making this very easily maintainable and expandable. Thanks for any help or advice I can get.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨敛清荷 2024-12-14 08:42:38

如果您问当前 Activity 了解其正在读取的 ContentProvider 中的数据已更改的最佳方法是什么,那么答案是 ContentObserver。

举例来说,假设您有一组 Activity 和 ContentProvider。活动 A 是来自内容提供程序 A 的项目的列表视图,并且该列表视图是通过某种 CursorAdapter 填充的。每当您对 ContentProviders CONTENT_URI 进行查询时,游标应通过 ContentObserver 自动监视 CONTENT_URI 的更改。

其工作原理是因为在 ContentProvider 中:

  1. 您的查询方法在光标上调用 setNotificationUri 来查询所查询的 URI。
  2. 每当插入/更新/删除方法中发生更改时,您都会在 URI 上调用 notificationChange。

如果您的服务中存在可以更改且未绑定到 ContentProvider 的其他数据,那么您在如何传递数据方面有多种选择。如果 Activity 与您的服务位于同一个 APK 中,那么您可以使用某种静态变量、应用程序上下文或其他自定义通信形式。这里的方法相当自由,因为您在同一个虚拟机中运行,因此您有很多可用的东西。

在您的情况下,听起来大多数活动都将位于单独的 APK 中。如果是这种情况,您可能希望发送广播 Intent,让整个系统知道这些更改。如果这些更改仅在 Activity 运行时起作用,您可以在关心更改的 Activity 的生命周期内注册一个 BroadcastReceiver。或者,您可以将 BroadcastReceiver 放入应用程序清单中,并始终接收该更改。

这让我们想到了 IntentServices。 IntentServices 是处理 Android 中长时间运行任务的“最简单”方法。他们所做的就是创建一个后台线程来处理发送到服务中的意图。常见的用例如下:

  1. 您收到需要做出反应的广播意图(这将需要一段时间)将
  2. 定向意图(具有设置组件名称的意图)发送到 IntentService,其中包含来自广播意图的一些信息。
  3. IntentService 要么生成一个新的 BG 线程,要么将意图添加到现有 BG 线程上的处理队列中。
  4. 在 IntentService 中调用 onHandleIntent 函数,以便您现在可以使用所需的处理时间来处理该更改。

If you are asking what the best way is for a current Activity to know data has changed in the ContentProvider it is reading from, than the answer is a ContentObserver.

Lets say, for examples sake, you have a set of Activities and ContentProviders. Activity A is a list view of items from Content Provider A, and that list view is populated via a CursorAdapter of some kind. Whenever you have a query on a ContentProviders CONTENT_URI, the cursor should automatically monitor the CONTENT_URI for changes via a ContentObserver.

The reason this works is because in the ContentProvider:

  1. Your query method calls setNotificationUri on your cursor to the URI that was queried.
  2. Whenever a change happens in an insert/update/delete method, you are calling notifyChange on the URI.

If there is additional data that can change in your service that is not tied to a ContentProvider, than you have a few choices on how to pass data. If the Activity is in the same APK as your service, than you could use some sort of static variable, or the application context, or some other custom form of communication. The methods are rather liberal here because you are running in the same VM, so you have a lot of things available to you.

In your situation, it sounds like most activities will be in separate APKs. If that is the case, you will probably want to send out broadcast Intents to let the entire system knwo about the changes. If these changes only matter when an activity is running, you can register a BroadcastReceiver for the life of the Activity that cares about the change. Alternatively, you could put a BroadcastReceiver in the applications manifest, and always receive that change.

That brings us to IntentServices. IntentServices are the "easiest" way to handle long running tasks in Android. All they do, is create a background thread that will handle intents being sent into the service. A common use case is as follows:

  1. You receive a broadcast intent that you need to react to (and it will take a while)
  2. Send a directed intent (intent with a set component name) to an IntentService with some info from the broadcast intent.
  3. IntentService either spawns a new BG thread, or adds the intent to the processing queue on the existing BG thread
  4. The onHandleIntent function is called in the IntentService so that you can now process that change using as much processing time as you want.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文