从服务访问UI线程处理器 [英] Accessing UI thread handler from a service

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

问题描述

我在尝试一些新的东西在Android上的,我需要访问UI线程的处理程序。

I am trying some thing new on Android for which I need to access the handler of the UI thread.

我了解以下内容:

  1. 在用户界面线程都有自己的处理程序 和弯针
  2. 在所有邮件将被放 到用户界面的消息队列 螺纹
  3. 活套拿起事件 并通过它来处理程序
  4. 的处理程序处理的消息, 发送specfic事件到UI
  1. The UI thread has its own handler and looper
  2. Any message will be put into the message queue of the UI thread
  3. The looper picks up the event and passed it to the handler
  4. The handler handles the message and sends the specfic event to the UI

我想有我的服务,已经拿到UI线程处理程序,并把消息到这个处理程序。 所以,这个消息将会被处理,并会发出到用户界面。 这里的服务将是一个正常的服务,将通过一些应用被启动。

I want to have my service which has to get the UI thread handler and put a message into this handler. So that this message will be processed and will be issued to the UI. Here the service will be a normal service which will be started by some application.

我想知道这是可能的。 如果是的话,请提出一些code片段,这样我可以尝试一下。

I would like to know if this is possible. If so please suggest some code snippets, so that I can try it.

问候 吉里什

推荐答案

我相信这个片段的code构造与主(UI)线程相关的处理程序:

I believe this snippet of code constructs a Handler associated with the main (UI) thread:

Handler handler = new Handler(Looper.getMainLooper());

您可以然后张贴的东西执行主(UI)线程像这样:

You can then post stuff for execution in the main (UI) thread like so:

handler.post(something_to_run_on_main_thread);

我怀疑这是不是真的正常的服务需要这种功能。其一它甚至没有在顶级 Android开发指南上的进程中提到和线程。如果你有几分钟的空闲时间,随意详细一点的用例。也许服务直接摆弄东西,实际上应该通过一个活动来处理? (活动可以使用<一个href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29">Activity.runOnUiThread().)

I suspect it's not really normal for a Service to need this kind of functionality. For one it's not even mentioned in the top-level Android Dev Guide on processes and threads. If you have a few minutes to spare, feel free to elaborate a bit on the use case. Maybe the Service it tinkering directly with something that should actually be handled by an Activity? (Activities can use Activity.runOnUiThread().)

这篇关于从服务访问UI线程处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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