在开始使用显式VS隐含的意图机器人服务意图、机器人、VS

2023-09-06 13:49:16 作者:一战定江山

根据标准Android文档中,prefered的方式来启动服务(启动服务,是)是使用一个明确的意图是这样的:

According to the standard Android documentation, the prefered way to start a service (started service that is) is to use an explicit intent like this:

// Using explicit intent:
Intent serviceIntent = new Intent(getApplicationContext(), MyService.class);
// or:
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);

您还可以启动/使用隐含的意图,在清单中指定的操作字符串停止服务,像这样的:

You can also start/stop a service using an implicit intent with an action string specified in the manifest, like this:

// Using implicit intent:
static final String serviceAction = "com.example.my.app.services.MYSERVICE";
Intent serviceIntent = new Intent(serviceAction);
startService(serviceIntent);

// AndroidManifest.xml:
<service android:name="com.example.my.app.services.MyService"
   android:exported="false" android:process=":services" >
   <intent-filter>
      <!-- Start/Stop service -->
      <action android:name="com.example.my.app.services.MYSERVICE" />
   </intent-filter>
</service>

在该服务仅在本地使用(第三方应用程序不能启动或绑定到它),该文件说,你不应该包括意图过滤器在清单中的服务标签,并应设置在导出标记为false。

When the service is used only locally (third party applications are not allowed to start or bind to it), the documentation says that you should not include an intent-filter in the manifest service tag and you should set the exported tag to false.

注:活动和在单独的进程中运行的服务(应用及:服务流程)。活动和服务之间的通信是通过实现的AIDL接口做(这样做,因为只有AIDL远程接口可以让我需要simultanously处理工控机,不仅是活动的,但大多是在正在运行的服务之间的服务中做多线程:服务过程)。

Note: the activities and services run in separate processes (:application and :services processes). The communication between activity and service is done by implementing AIDL interfaces (this is done because only AIDL remote interfacing allows me to do multi-threading within the service that needs to handle IPC simultanously, not only between activities but mostly between services running within the :services process).

我的问题是:

Q1:什么时候我用我的应用程序的活动和服务,在两个不同的进程中运行,是否需要在明确意图来使用隐式意图来启动和停止服务

Q1: When the activities and services I use in my app are run in two different processes, do I need to use implicit intents over explicit intents to start and stop the services?

Q2:当:应用程序消失(被破坏,而不是在内存中了)和:服务进程在后台运行,我怎么从一个新的重新连接:申请过程中对已经运行:服务过程?不知怎的,我需要得到一个参考:服务过程中再次让我可以停止进程中正在运行的服务。这不能使用AIDL AFAIK做可以。

Q2: When the :application process is gone (destroyed, not in memory anymore) and the :services process is running in the background, how do I connect again from a new :application process to the already running :services process? Somehow I need to get a reference to the :services process again so that I can stop the running service inside that process. This cannot be done using AIDL afaik.

问题是,机器人能够而且将会破坏:时容易出资源的应用程序,这是罚款由我只要在:服务过程中保持运行。 (是的,我知道通过设置服务为前台服务影响的过程中,等我也可以阅读说明书;),但是这不是我的问题)。

The problem is that Android can and will destroy the :application process easily when out of resources, and that is fine by me as long as the :services process keeps running. (Yes, I know about influencing the process by setting the service as a foreground service, etc. I too can read manuals ;) but that is not my problem).

我找不到有关我的问题的任何信息或回答时,活动和服务的分离过程和使用AIDL,而当:申请过程中需要找到:服务进程再次它已被杀害的Andr​​oid后,或者当用户再次进入该应用(在他/她之前离开该应用)。

I cannot find any information or answers related to my questions when the activities and services are in separated processes and use AIDL, AND when the :application process needs to "find" the :services process again after it has been killed by Android or when the user enters the app again (after he/she left the app before).

任何专家级的建议是值得欢迎的。

Any expert-level advise is welcome.

推荐答案

A1:尽管不同进程中的活动和服务来看,他们仍然属于同一个应用程序。您仍然可以使用明确的意图,我看不出这里使用隐式意图的任何具体的优势(让我知道如果发现任何:))

A1 : Even though your activity and service run in different processes they still belong to same Application. You can still use explicit intent, I don't see any specific advantage of using implicit intent here (let me know if find any :) )

A2:让我列举下几个事实此处

A2 : let me list down few facts here

的开始的服务(而不是绑定主编服务)生命周期独立活动的已启动该服务的生命周期。这是真实的,不论天气两者都在同一过程或不同的进程中运行 服务只有一个实例会活着,在任何时间点。当你的活动调用startService(),服务实例会,如果它尚未运行,创建(在这种情况下,你的服务将收到的onCreate()回调为好)。但是,如果服务已在运行,框架将只需调用onStartCommand()上的回调已经运行的进程(在这种情况下没有的onCreate()回调)。同样这一切都是真实的,不论活动和服务上的相同过程或不同的进程正在运行。

现在回答你的问题,如果你的服务是否仍在运行(因为startService()以previous活动通话),然后bindService()/ startService()将确保连接到现有的服务。

Now to answer your question, if you service is still running (because of startService() call by previous activity), then bindService()/startService() will make sure to connect to existing service.

希望这是对你有所帮助。让我知道如果您有任何其他具体问题。

Hope this is of some help to you. Let me know if you have any other specific questions.