在单独的进程android中启动服务 [英] Start a service in a separate process android
问题描述
我想在一个单独的进程中启动一个服务(即当我在设置中转到我的应用程序管理器然后去运行服务时,它应该在一个单独的进程中显示我的服务).
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).
我的Android Manifest如下:
My Android Manifest is as follows:
<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>
我在 MainActivity 中启动服务,很明显,当我终止应用程序时,服务也会关闭.谁能告诉我如何将服务作为不同的进程启动.
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.
推荐答案
检查 AndroidManifest.xml
中 service
的 process
属性.您需要将 android:process
值更改为以 :
开头.
Check out the process
attribute for service
in AndroidManifest.xml
. You need to change your android:process
value to start with a :
.
http://developer.android.com/guide/topics/manifest/service-element.html
相关部分:
如果分配给该属性的名称以冒号 (':') 开头,则会在需要时创建一个新的应用程序私有进程,并且服务在该进程中运行.如果进程名称以小写字符开头,则该服务将在该名称的全局进程中运行,前提是它有权这样做.这允许不同应用程序中的组件共享一个进程,从而减少资源使用.
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.
定义服务流程
android:process
字段定义了要运行服务的进程的名称.通常,应用程序的所有组件都在为应用程序创建的默认进程中运行.但是,组件可以使用自己的进程属性覆盖默认值,从而允许您将应用程序分布到多个进程中.
The android:process
field 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.
如果分配给该属性的名称以冒号 (':') 开头,则该服务将在其自己的单独进程中运行.
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.
这篇关于在单独的进程android中启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!