简体   繁体   English

将活动中的数据发送到第三方服务

[英]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 从我聚集在这里开始,我必须

  • create a Service , which I'll start in my MainActivity right before finish() it 创建一个Service ,我将在finish()之前在MainActivity中启动它
  • declare an IntentFilter and addAction() my custom string to identify my action 声明一个IntentFilteraddAction()我的自定义字符串以标识我的操作
  • registerReceiver() my receiver and my filter registerReceiver()我的接收器和我的过滤器

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: 问题:

  1. First of all, is this the correct modus operandi? 首先,这是正确的作案手法吗?
  2. My Service is running in the same thread as my Activity, right? 我的服务与我的活动在同一线程中运行,对吗? So what happens if I finish() my MainActivity? 那么,如果我完成 MainActivity ()会怎样? What can I do to keep my service always running in background no matter what? 无论如何,如何使我的服务始终在后台运行?
  3. In any app where I want to send data to be received from my service, do I just have to create an Intent("My_Action") (the same string I'm expecting on the other side) and do sendBroadcast(intent) ? 在任何我想发送要从服务接收数据的应用程序中,我是否只需要创建一个Intent(“ My_Action”) (另一端希望与之相同的字符串)并执行sendBroadcast(intent)
  4. Should I use Service or IntentService? 我应该使用Service还是IntentService? What's better in my circumstance? 我的情况下有什么更好的方法?
  5. Do I have to write anything in the Manifest, either in my Service or in the other 3rd-part apps? 我是否需要在清单中编写任何内容,无论是在“服务”中还是在其他第三部分应用程序中?

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 非常感谢您的专业知识,因为此服务将是许多机器上至关重要的部分,所以我想确保我以最好的方式做到这一点,而不仅仅是一些不稳定的工作代码

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

  2. 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. 如果您希望服务运行(无论如何),则必须在服务内部获取唤醒锁。

  3. 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. 我不确定您是否要求其他应用程序能够通过您的服务发送数据。

  4. See Service vs IntentService or What is the difference between an IntentService and a Service? 请参阅服务与IntentService,或者IntentService和Service有什么区别?

  5. 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.htmlhttp://developer.android.com/guide/components/services.html也许您在询问有关定义开始意图的更多信息服务( Manifest.xml中的服务目的过滤器的目的 )?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务发送到活动的数据不起作用 - Data send from service to activity not working 如何设置一个意图,以便我可以将数据发送到第二个活动,然后从那里发送到第三个活动? - How to set an intent so that I can send a data to the second activity and from there to the third activity? 将数据从第二个活动传递到第三个活动 - Passing data from second activity to third activity 从已经运行的粘性服务向活动发送数据 - Send data to activity from already running sticky service 如何将字符串数据从 Activity 发送到启动服务 onclick 方法? - How to send string data from Activity to started Service onclick method? Android-如何将数据从活动发送到服务? - Android- How to send data from activity to service? LocalBroadcastManager 现已弃用,如何将数据从服务发送到活动? - LocalBroadcastManager is now deprecated, how to send data from service to activity? 将数据从第三个活动转移到第一个活动 - Transferring data from the third activity to the first 将数据从第三个活动发送到第一个活动 - 正确的方法? - Sending data from Third Activity to First Activity - correct method? 如何正确地将双打从服务发送到活动 - How to correctly send doubles from service to activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM