gpt4 book ai didi

android - 服务中的可运行对象是否在 UI 线程上运行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:35 25 4
gpt4 key购买 nike

在 Android 中,当我在服务中创建一个 runnable 并运行它时,虽然我意识到它在自己的线程中运行,但这个线程是否以某种方式成为 UI 线程的一部分?换句话说,如果runnable执行的过程很长,会不会影响UI?

编辑:

private class SomeRunnable implements Runnable
{
@Override
public void run()
{
try
{

}
}
}

SomeRunnable runnable = new SomeRunnable();
(new Handler()).postDelayed(runnable, 1000);

最佳答案

文档:

A services runs in the same process as the application in which it is declared and in the main thread of that application,

不同线程:

Thread t = new Thread(new MyRunnable());
t.start();

UI/服务线程:

Handler h = new Handler();
h.post(new MyRunnable());

关于android - 服务中的可运行对象是否在 UI 线程上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14808640/

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