[英]Send data from an activity to a third part service
First of all, I want to apologize, since I'm pretty sure all the informations I need are already somewhere here on stackoverflow. 首先,我要道歉,因为我非常确定我需要的所有信息已经在stackoverflow上的某处。 Problem is, I found quite similar situations like mine, but not exactly this one, so I thought about asking about the correct modus-operandi to create a final "how-to" for this circumstance, to help future devs 问题是,我发现了与我非常相似的情况,但不完全是这种情况,因此我考虑询问正确的操作方法,以针对这种情况创建最终的“操作方法”,以帮助未来的开发人员
I need to write a background service, UI-less, which will send and receive data from third part applications (which already exist, I just have to include inside the code to send/receive the data shared with my service) 我需要编写一个无UI的后台服务,该服务将发送和接收来自第三方应用程序的数据(已经存在,我只需要在代码内部包括发送/接收与我的服务共享的数据)
Let's focus on the App-->send-->Service flow, since I guess the other way around is the same 让我们专注于App-> send-> Service flow,因为我猜相反
From what I gathered here, I have to 从我聚集在这里开始,我必须
In the onReceive() of my BroadcastReceiver, I'll check if my intent.getAction() has the same string I was expecting (because Broadcasts are system-wide and is virtually send could intercept them, right?) - if so, I'll do my business 在我的BroadcastReceiver的onReceive()中,我将检查我的intent.getAction()是否具有与我期望的相同的字符串(因为Broadcast是系统范围的,并且实际上是send可以拦截它们,对吗?)-如果是这样,我我去做生意
QUESTIONS: 问题:
Thanks a lot for your expertise, since this service will be a crucial part on a lot of machines I wanna be sure I'm doing it the best way, and not just some unstable make-it-work code 非常感谢您的专业知识,因为此服务将是许多机器上至关重要的部分,所以我想确保我以最好的方式做到这一点,而不仅仅是一些不稳定的工作代码
Depending on how you want the "send data" process to be started, there are different approaches. 根据您希望启动“发送数据”过程的方式,有不同的方法。 It appears the send is short lived - so an IntentService is appropriate. 看来发送是短暂的-因此IntentService是合适的。 If always from some user action in your activity, just call startService. 如果总是通过活动中的某些用户操作,则只需调用startService。 If exposing send so other apps (not yours) can use it see Create an android service which can be called only by certain apps . 如果公开发送,以便其他应用程序(而非您的应用程序)可以使用它,请参阅创建只能由某些应用程序调用的android服务 。 There are use cases where starting the service is via a broadcast - see https://blog.nraboy.com/2014/10/use-broadcast-receiver-background-services-android/ . 在某些情况下,通过广播启动服务-请参阅https://blog.nraboy.com/2014/10/use-broadcast-receiver-background-services-android/ 。
Yes, services run on app (not tied to any activity) thread. 是的,服务在应用程序线程上运行(与任何活动无关)。 Finishing an activity has nothing to do with a running (started) service - the service will continue to run. 完成活动与正在运行的(已启动)服务无关—该服务将继续运行。 If you want the service to run (no matter what), you have to obtain a wake lock inside your service. 如果您希望服务运行(无论如何),则必须在服务内部获取唤醒锁。
If you want to send data (via service) from inside your app, just call startService(). 如果要从应用程序内部通过服务发送数据,只需调用startService()。 I'm not sure if you are asking for other apps to be able to send data via your service. 我不确定您是否要求其他应用程序能够通过您的服务发送数据。
See Service vs IntentService or What is the difference between an IntentService and a Service? 请参阅服务与IntentService,或者IntentService和Service有什么区别?
A service must be defined in your manifest. 必须在清单中定义服务。 See http://developer.android.com/guide/topics/manifest/service-element.html and http://developer.android.com/guide/components/services.html Maybe you are asking more about defining intents to start the service ( Purpose of Service Intent-Filter inside Manifest.xml )? 请参阅http://developer.android.com/guide/topics/manifest/service-element.html和http://developer.android.com/guide/components/services.html也许您在询问有关定义开始意图的更多信息服务( Manifest.xml中的服务目的过滤器的目的 )?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.