looper

Handler-Looper implementation in Android

做~自己de王妃 提交于 2019-12-01 09:02:15
I have Activity with Handler (UI thread) I start new Thread and make handler.post(new MyRunnable()) - (new work thread) Android documentation said about post method: "Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached." Handler attached to UI thread. How android can run runnable in the same UI thread without new thread creation? Is new thread will be created using Runnable from handler.post()? Or it's only run() method will be called from Runnable subclass? Handler attached to UI thread. Correct. How android can run

Handler-Looper implementation in Android

瘦欲@ 提交于 2019-12-01 06:06:44
问题 I have Activity with Handler (UI thread) I start new Thread and make handler.post(new MyRunnable()) - (new work thread) Android documentation said about post method: "Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached." Handler attached to UI thread. How android can run runnable in the same UI thread without new thread creation? Is new thread will be created using Runnable from handler.post()? Or it's only run()

Android的消息处理机制(图+源码分析)——Looper,Handler,Message

一笑奈何 提交于 2019-11-29 22:41:21
作为一个大三的预备程序员,我学习android的一大乐趣是可以通过源码学习google大牛们的设计思想。android源码中包含了大量的设 计模式,除此以外,android sdk还精心为我们设计了各种helper类,对于和我一样渴望水平得到进阶的人来说,都太值得一读了。这不,前几天为了了解android的消息处理机 制,我看了 Looper,Handler,Message 这几个类的源码,结果又一次被googler的设计震撼了,特与大家分享。 android的消息处理有三个核心类:Looper,Handler和Message。其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道,因此我没将其作为核心类。下面一一介绍: 线程的魔法师 Looper Looper的字面意思是“循环者”,它被设计用来使一个普通线程变成 Looper线程 。所谓Looper线程就是循环工作的线程。在程序开发中(尤其是GUI开发中),我们经常会需要一个线程不断循环,一旦有新任务则执行,执行完继续等待下一个任务,这就是Looper线程。使用Looper类创建Looper线程很简单: public class LooperThread extends Thread { @Override public void run() { //

android的消息处理机制(图+源码分析)——Looper,Handler,Message

随声附和 提交于 2019-11-29 22:41:05
作为一个大三的预备程序员,我学习android的一大乐趣是可以通过源码学习google大牛们的设计思想。android源码中包含了大量的设计模式,除此以外,android sdk还精心为我们设计了各种helper类,对于和我一样渴望水平得到进阶的人来说,都太值得一读了。这不,前几天为了了解android的消息处理机制,我看了 Looper,Handler,Message 这几个类的源码,结果又一次被googler的设计震撼了,特与大家分享。 android的消息处理有三个核心类:Looper,Handler和Message。其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道,因此我没将其作为核心类。下面一一介绍: 线程的魔法师 Looper Looper的字面意思是“循环者”,它被设计用来使一个普通线程变成 Looper线程 。所谓Looper线程就是循环工作的线程。在程序开发中(尤其是GUI开发中),我们经常会需要一个线程不断循环,一旦有新任务则执行,执行完继续等待下一个任务,这就是Looper线程。使用Looper类创建Looper线程很简单: public class LooperThread extends Thread { @Override public void run() { //

Android 第十四课——Handler Looper Message

江枫思渺然 提交于 2019-11-29 22:40:46
1、基础概念 1)android.os.Handler 2)主要接受子线程发送的数据, 并用此数据配合主线程更新UI. 3)应用程序一旦启动,Android UI 这个主线程的生命周期就开始了,然而,Android UI 线程并不是线程安全的,也就是说,更新UI只能在主线程中同步更新,子线程中异步操作是危险的。所以,项目中如果我们直接new Thread 内部去更改Android UI,往往会报错误如下: java.lang.RuntimeException: Can't create handler inside thread that has not called 为了避免错误发生,那么更新Android UI 必须在主线程中操作,具体如何操作呢?Handler就发挥了特的作用。Handler的使用同时也会带来许多相关的知识,下面一一讲解。 2、实例化 1)采用默认的方式 new Handler();项目中很少使用这种方式。如果他是主线程中运行的,那么这个handler对象内部绑定了一个Looper对象Looper.getMainLooper();Looper对象是用来干嘛的呢? Looper类是用来为一个线程开启一个消息循环。可以这样获取对应的线程:handler.getLooper().getThread()。默认情况下Android中新诞生的线程是没有开启消息循环的

Python 初级 6 循环 (二)

我只是一个虾纸丫 提交于 2019-11-29 11:15:38
一、复习 1 for循环 for looper in [1, 2, 3, 4]: print("hello") 1) 每次循环开始,会依次把列表中的数按顺序赋值给looper,第一次looper为1,第二次为2,第三次为3,第四次为4 2) 每赋一次值,就会把下面指令块的所有工作执行一遍,这个指令块也叫循环体 3) 每次执行完后,变量looper会赋上列表中下一个值,直到赋完为止 2 break跳出 3 range()函数 1)range(1, 5)表示的为列表[1, 2, 3, 4],右边的5表示边界,不是具体的值 2)range通常我们写成range(0, 4), 表示列表[0,1, 2, 3],这也是计算机通常表示数的方法,从0这个具体的数开始,到边界4结束(不包括边界4)一共有4个数 3) range简化版,range(4)即表示range(0, 4) 4 字符串格式化:占位符 print("{},{}".format(1, 2)) 来源: https://www.cnblogs.com/luhouxiang/p/11516094.html

A WebView in a thread can't be created

时光怂恿深爱的人放手 提交于 2019-11-29 10:56:09
i have some threads in which i create some views and prepare them to be displayed. Among them i also have a WebView. This code is executed in thread: WebView lGraphWebView = null; try{ lGraphWebView = new WebView(AppController.getAppController()); }catch (Exception e) { Log.d("info", "error: " +e ); } and it throws the following exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() This is a bit strange, because when i create a simple button all is OK. So, can anyone explane to me why on creation of a WebView i get this exception and if

A WebView in a thread can't be created

做~自己de王妃 提交于 2019-11-28 04:26:35
问题 i have some threads in which i create some views and prepare them to be displayed. Among them i also have a WebView. This code is executed in thread: WebView lGraphWebView = null; try{ lGraphWebView = new WebView(AppController.getAppController()); }catch (Exception e) { Log.d("info", "error: " +e ); } and it throws the following exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() This is a bit strange, because when i create a simple

深入解读Linux与Android的相互关系& Android消息处理系统的原理

 ̄綄美尐妖づ 提交于 2019-11-27 15:33:53
深入解读Linux与Android的相互关系 大家都知道Android是基于Linux内核的 操作系统 ,也曾经和Linux基金会因为内核问题产生过分歧,本文将开始对Android的内核进行剖析,主要介绍Android和Linux之间的关系,后续还会讲到Android系统在Linux系统之上扩展的部分功能和驱动,希望大家能够持续关注IT168技术频道。 虽然Android基于Linux内核,但是它与Linux之间还是有很大的差别,比如Android在Linux内核的基础上添加了自己所特有的驱动程序。下面我们就来分析一下它们之间究竟有什么关系? 一、 Android为什么会选择Linux 成熟的 操作系统 有很多,但是Android为什么选择采用Linux内核呢?这就与Linux的一些特性有关了,比如: 1、强大的 内存 管理和进程管理方案 2、基于权限的安全模式 3、支持共享库 4、经过认证的驱动模型 5、Linux本身就是开源项目 更多关于上述特性的信息可以参考Linux 2.6版内核的官方文档,这便于我们在后面的学习中更好地理解Android所特有的功能特性。接下来分析Android与Linux的关系。 二、Android不是Linux 看到这个标题大家可能会有些迷惑,前面不是一直说Android是基于Linux内核的吗,怎么现在又不是Linux了?迷惑也是正常的

How to create a Looper thread, then send it a message immediately?

拥有回忆 提交于 2019-11-27 05:56:20
I have a worker thread that sits in the background, processing messages. Something like this: class Worker extends Thread { public volatile Handler handler; // actually private, of course public void run() { Looper.prepare(); mHandler = new Handler() { // the Handler hooks up to the current Thread public boolean handleMessage(Message msg) { // ... } }; Looper.loop(); } } From the main thread (UI thread, not that it matters) I would like to do something like this: Worker worker = new Worker(); worker.start(); worker.handler.sendMessage(...); The trouble is that this sets me up for a beautiful