Android 应用程序和服务中的线程
Android threads in app and service
我有一个 Android 应用程序,它有一个长期 运行 服务。
线程(Android 在其中调用服务的方法)是否与 Android 用于调用应用程序方法(对于 Activity 等)的线程相同?
如果是这样,是否可以在某处保证?
根据 android 文档:
A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.
要在单独的线程中处理服务请求,您需要直接操作线程。或者您可以扩展 IntentService.
您也可以在单独的进程中启动服务。为此,您需要在清单中声明它:
<service
android:name="WordService"
android:process=":my_process"
android:icon="@drawable/icon"
android:label="@string/service_name"
>
</service>
您可以在此处找到更多信息:
http://developer.android.com/guide/components/services.html
我有一个 Android 应用程序,它有一个长期 运行 服务。
线程(Android 在其中调用服务的方法)是否与 Android 用于调用应用程序方法(对于 Activity 等)的线程相同?
如果是这样,是否可以在某处保证?
根据 android 文档:
A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.
要在单独的线程中处理服务请求,您需要直接操作线程。或者您可以扩展 IntentService.
您也可以在单独的进程中启动服务。为此,您需要在清单中声明它:
<service
android:name="WordService"
android:process=":my_process"
android:icon="@drawable/icon"
android:label="@string/service_name"
>
</service>
您可以在此处找到更多信息: http://developer.android.com/guide/components/services.html