是否在服务运行可运行在UI线程 [英] Does a runnable in a service run on the UI thread

查看:166
本文介绍了是否在服务运行可运行在UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的,当我创建一个可运行的一个服务内部并运行它,虽然我知道它运行在自己的线程,这是线程UI线程莫名其妙的一部分?换句话说,如果可运行进行了漫长的过程,将它影响用户界面

编辑:

 私有类SomeRunnable实现Runnable
{
  @覆盖
  公共无效的run()
  {
    尝试
    {

    }
  }
}

SomeRunnable可运行=新SomeRunnable();
(新处理器())postDelayed(可运行,1000)。
 

解决方案

文档:

  

一个服务运行在相同的过程中应用,其中它是   声明并在该应用程序的主线程,

不同的线程:

 线程t =新主题(新MyRunnable());
t.start();
 

UI /服务主题:

 处理程序H =新的处理程序();
h.postRunnable(新MyRunnable());
 

In Android, when I create a runnable inside a service and run it, while I realize it runs in its own thread, is this thread somehow part of the UI thread? In other words, if the runnable carried out a long process, would it affect the UI?

EDIT:

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

    }
  }
}

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

解决方案

Docs:

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

Different thread:

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

UI/Service Thread:

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

这篇关于是否在服务运行可运行在UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆