gpt4 book ai didi

应用程序和服务中的 Android 线程

转载 作者:行者123 更新时间:2023-11-30 01:56:50 25 4
gpt4 key购买 nike

我有一个 Android 应用程序,它有一个长期运行的服务。

线程(Android 调用服务方法的线程)是否与 Android 用于调用应用程序方法(用于 Activity 等)的线程相同?

如果是这样,是否可以在某处保证?

最佳答案

根据安卓文档:

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 .

您也可以在单独的进程中启动服务。为此,您需要在 list 中声明它:

<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 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32088301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com