应用程序间通信的最佳方法而又不突出它们? [英] Best approach for inter-app communcation without foregrounding them?

查看:86
本文介绍了应用程序间通信的最佳方法而又不突出它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力,如何在不使每个应用程序都出现前景的​​情况下最好地实现应用程序间通信.我曾考虑过使用BroadcastReceivers在两个应用程序之间进行通信,但是看来它们必须在清单中进行注册,这使得将数据带到活动中变得很困难.一个示例是一个应用程序会将其状态发送给另一个应用程序,表明它已准备好开始运行会话.但是,我希望在启动会话时将另一个应用程序保留在后台,并让用户根据自己的意愿在应用程序之间进行切换.

I have been struggling on how what would be the best approach for inter-app communication without foregrounding each app. I thought about using BroadcastReceivers to communicate between the two apps, but it appears that they must be registered in the manifest, making it difficult to bring the data to the activities. An example would be that one app would send its status to another indicating that it is ready to start running a session. However, I would like to keep the other app in the background while I start the session and let the user switch between apps if he or she chooses.

有人知道什么是最好的方法吗?基本上,只要其他应用程序正在运行,我就希望将数据发送到另一个应用程序.

Does anyone know what would be the best approach? Basically, I would like to send data to the other app as long as the other app is running.

推荐答案

我想您对broadcastreceiver的想法还算不错: 您不必在清单中注册broadcastreceiver,也可以按需甚至使用Activity在组合中创建它们. 但是除此之外,还有一些方法可以解决您的问题:

I guess your thought about a broadcastreceiver isn't too bad at all: You don't have to register a broadcastreceiver in the manifest, you can also have them created onDemand or even on composition with your Activity. But besides that there are some ways to solve your problem:

方法1

首先,您可以使用IntentServicePendingIntent使用ResultReceiver进行简单的通信. 另请参见这个伟大的stackoverflow问题以获取更多信息:

First of all you could use a an IntentService and a PendingIntent to have a simple communication using a ResultReceiver. See also this great stackoverflow question for more information:

两个应用程序之间的android通信

方法2

更通用的方法可能是使用IntentFilter和一种方案来捕获发送数据.它也是基于Intent的,并且可以解决您的情况,如果(!)您可以正确地处理数据捕获:在您的情况下,仅当数据正在运行时. 另请参阅本文,以获取此方法的(简短)简短示例:

A more universal approach could be to use a IntentFilter and a scheme to catch send data. It's also Intent based and could be a solution for your scenario, if (!) you handle the catching of the data right: in your case only if it's running. See also this article for a (somewhat) short example of this approach:

https://android-developers.googleblog .com/2009/11/integrating-application-with-intents.html

方法3

最后但并非最不重要的一点:利用共享(但受保护!)文件和其他数据存储选项.当您的第二个应用程序运行时,这可能是一个加密文件和一个活动的侦听器.这可能还需要其他提到的选项的其他部分,例如可以由AlarmmanagerJobScheduler管理的正在运行的IntentService. 对于这种情况,我建议使用官方的Android文档:

And last but not least: make use of shared (but secured!) files and other data storage options. This could be an encrypted file and an active listener while your 2nd app is running. This would maybe also require other parts of the other mentioned options, like a running IntentService that could be managed by an Alarmmanager or JobScheduler. For such scenarios I'd recommend the official Android docs:

https://developer.android.com/training/secure-file-sharing/setup-sharing.html https://developer.android.com/guide/topics/data/data-storage.html

加号::由于在大多数情况下,您会获得某种后台运行服务", 我建议您看一下Android提供的一些方法,因为它们可以完全改变适合的方法和可能性:

Plus: since in most cases you'd have some sort of "background running service", I'd recommend you taking a look at some approaches Android gives you, since they could totally change the approach and possibilities that could fit:

https://www.bignerdranch. com/blog/在Android/中选择右侧背景调度程序

长话短说:

祝您好运,并随时通知我! ;)

Good luck and keep me posted! ;)

这篇关于应用程序间通信的最佳方法而又不突出它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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