在单独的进程android中启动服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22514373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:06:22  来源:igfitidea点击:

Start a service in a separate process android

androidmultithreadingserviceprocess

提问by SoH

I want to start a service in a separate process (i.e when I go to my Application manager in the settings and then go to running services, it should show my service in a separate process).

我想在一个单独的进程中启动一个服务(即当我在设置中转到我的应用程序管理器然后去运行服务时,它应该在一个单独的进程中显示我的服务)。

My Android Manifest is as follows:

我的Android清单如下:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.timerapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name="com.example.timerapp.WorkerThread"
        android:process="com.moizali"></service>
</application>

I am starting the service in my MainActivity so obviously when I kill the application the service shuts down as well. Can anyone tell me how to start the service as a different process.

我正在我的 MainActivity 中启动服务,所以很明显,当我终止应用程序时,服务也会关闭。谁能告诉我如何将服务作为不同的进程启动。

回答by adao7000

Check out the processattribute for servicein AndroidManifest.xml. You need to change your android:processvalue to start with a :.

检查in的process属性。您需要更改您的值以从.serviceAndroidManifest.xmlandroid:process:

http://developer.android.com/guide/topics/manifest/service-element.html

http://developer.android.com/guide/topics/manifest/service-element.html

The relevant section:

相关部分:

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

如果分配给该属性的名称以冒号 (':') 开头,则会在需要时创建一个新进程,该进程私有的应用程序将在该进程中运行该服务。如果进程名称以小写字符开头,则该服务将在具有该名称的全局进程中运行,前提是它有权这样做。这允许不同应用程序中的组件共享一个进程,从而减少资源使用。

The other answer provided doesn't really answer the question of how to start a service in a separate process.

提供的另一个答案并没有真正回答如何在单独的进程中启动服务的问题。



Defining a Process of a Service

定义服务进程

The android:processfield defines the name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. However, a component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

android:process字段定义了要运行服务的进程的名称。通常,应用程序的所有组件都在为应用程序创建的默认进程中运行。但是,组件可以使用自己的进程属性覆盖默认值,从而允许您将应用程序分布在多个进程中。

If the name assigned to this attribute begins with a colon (':'), the service will run in its own separate process.

如果分配给该属性的名称以冒号 (':') 开头,则该服务将在其自己的单独进程中运行。

<service
  android:name="com.example.appName"
  android:process=":externalProcess" />

If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

如果进程名称以小写字符开头,则该服务将在具有该名称的全局进程中运行,前提是它有权这样做。这允许不同应用程序中的组件共享一个进程,从而减少资源使用。

回答by Amir

Running on a separate process will not solve your problem. I had the same issue and this is a normal behavior of Android. When you start a Service(not a Foreground Service), even if it is in a separate process, the OS can kill it any time. In your case, if you close the Activity(s)/kill the Application, the OS will normally close the service even if they are in separate processes. You have two options:

在单独的进程上运行不会解决您的问题。我有同样的问题,这是 Android 的正常行为。当您启动一个Service(不是 Foreground Service)时,即使它在一个单独的进程中,操作系统也可以随时终止它。在您的情况下,如果您关闭Activity(s)/kill the Application,操作系统通常会关闭该服务,即使它们位于不同的进程中。您有两个选择:

1- Start your service as a Foreground Service. In this case the Servicewill not be closed due to almost any condition. Be aware that the foreground service is designed for specific applications and you will have a sticky notification in the notification center and status bar.

1- 将您的服务作为ForegroundService启动。在这种情况下Service,几乎在任何情况下都不会关闭。请注意,前台服务是为特定应用程序设计的,您将在通知中心和状态栏中看到一个粘性通知。

2- Make your service as a Start service (not an IntentService). Then on the onStartCommandof service, return START_STICKY. This will tell the OS, if for any reason you need to close the Servicerun it again when you have enough resources. In your case, when the user closes the Activity/kill the Applicationthe Serviceprocess will be killed anyway, however it will normally reopen.

2- 将您的服务设为 Start 服务(而不是IntentService)。然后在onStartCommand服务,返回START_STICKY。这将告诉操作系统,如果出于任何原因您需要Service在有足够资源时再次关闭运行它。在你的情况下,当用户关闭Activity/杀ApplicationService过程中无论如何都会被杀死,但它通常会重新开放。