Android 判断当前thread 是否是UI thread

在Android 中判断当前的Thread是否是UI Thread 的方法:

  1.

if (Looper.myLooper() == Looper.getMainLooper()) {
    // Current thread is the UI/Main thread
}
 

2.

if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
    // Current thread is the UI/Main thread
}

参考如下文章:其中还讲到了  runOnUiThread()  方法的用法。

http://codetheory.in/checkdetect-whether-current-thread-uimain-thread-android/

http://lak4cyut.blogspot.jp/2011/03/android-tip-activityrunonuithread.html

时间: 2024-08-11 05:33:05

Android 判断当前thread 是否是UI thread的相关文章

Android Bitmap Processing Bitmaps Off the UI Thread(渣翻译)

二:UI线程外加载图像 1.使用AsyncTaskAsyncTask类提供了一个简单的后台处理方法并将结果返回到UI线程中.在线程中提议使用WeakReference<?>来缓存显示Bitmap的UI控件,如:ImageView.WeakReference能够确保即使在线程操作时,也能让资源回收器回收UI控件资源,如:ListView回收View资源的情况.ex: ImageView imageview = new ImageView(new LayoutParams(100, 100));

【Android】UI thread和非UI thread

参考http://www.cnblogs.com/mengdd/p/3418780.html UI线程即主线程,因其启动时与UI组件交互,所以称为UI线程 主要涉及android.widget and android.view两大组件

Android Non-UI to UI Thread Communications(Part 2 of 5)

Original:http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-2-of-5/ his is the second part of my series of blog posts on Android non-UI thread-to-UI thread communications. See herefor the start of the series.  As a refreshe

Android Non-UI to UI Thread Communications(Part 3 of 5)

Original:http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-3-of-5/ Continuing my series on Android non-UI thread-to-UI thread communications, this post covers use of the Handler Framework to facilitate the communications. 

Android Non-UI to UI Thread Communications(Part 1 of 5)

original:http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-1-of-5/ ANDROID UI THREAD AND ANR On the Android platform, applications operate, by default, on one thread.  This thread is called the UI thread.  It is often call

Android中的Handler, Looper, MessageQueue和Thread

Android中的Handler, Looper, MessageQueue和Thread 前几天,和同事探讨了一下Android中的消息机制,探究了消息的发送和接收过程以及与线程之间的关系.虽然我们经常使用这些基础的东西,但对于其内部原理的了解,能使我们更加容易.合理地架构系统,并避免一些低级错误. 对于这部分的内容,将分成4小节来描述: 1.职责与关系 2.消息循环 3.线程与更新 4.几点小结 ------------------------------------------------

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views 这个错误相信大家一定不陌生,特别是刚学习android时,会引起的错误,意思是指在子线程中对UI做了操作,现在写个简单的demo: public class MainActivity extends Activity { privat

Android开发笔记之:Handler Runnable与Thread的区别详解

在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run()方法就可以实现多线程操作了,但是一个类只能继承一个父类,这是此方法的局限. 下面看例子: 代码如下: package org.thread.demo; class MyThread extends Thread{ private String name; public MyThread(String

(android)Can&#39;t Create handler inside thread that has not called Looper.prepare()

接android游戏sdk中,经常会遇到Looper报错,此时需要在主线程中调用sdk函数. 将sdk的函数放到UI线程中执行. 如: activity.runOnUiThread(new Runnable() { @Override public void run() { } }); (android)Can't Create handler inside thread that has not called Looper.prepare()