handler

Handler post用法整理

末鹿安然 提交于 2020-01-01 21:02:34
来自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=197324 感谢原作者. /* * 在这个demo中,整个过程如下: 程序一启动,就把MyRunnable加入到消息队列中, android的handler是异步机制, 所以在handler.post(new MyRunnable()); 之后,程序会继续执行,所以以后的语句会继续, 这时候我们输出Oncreate中的当前线程ID 。 同时MyRunnable的run方法也在运行, 一样输出run方法中的当前线程ID,然后让线程休眠6秒 。 */ 代码片段,双击复制 package xmren.handler.app; import xmren.handlerdemo.app . R; import android.app.Activity; import android . os.Bundle; import android . os.Handler; public class HandlerdemoActivity extends Activity { private Handler handler= new Handler(); private myrunnable runnable= new myrunnable(); /** Called when the

Handler消息传送机制

只愿长相守 提交于 2020-01-01 21:02:18
一、什么是UI线程 当程序第一次启动的时候,Android会同时启动一条主线程( Main Thread)。 主要负责处理与UI相关的事件。 二、UI线程存在的问题 出于性能优化考虑,Android的UI操作并不是线程安全的,意味着如果多个线程并发操作UI线程,可能导致线程安全问题。 为了解决这个问题,Android规定:只允许UI线程修改Activity里的UI组建。 三、消息传送机制作用 为了解决Android应用多线程问题—Android平台只允许UI线程修改Activity里的UI组建,就会导致新启动的线程无法改变界面组建的属性值。 四、Handler的作用(子线程往主线程发送消息的情况) 在新启动的线程中发送消息。 使用Handler对象的sendMessage()方法或者SendEmptyMessage()方法发送消息。 在主线程中获取 处理消息。 重写Handler类中处理消息的方法(void handleMessage(Message msg)),当新启动的线程发送消息时,消息发送到与之关联的MessageQueue。而Hanlder不断地从MessageQueue中获取并处理消息。 五、Hanlder类 Handler类包含如下方法用于发送、处理消息: void handleMessage(Message msg):处理消息的方法,该方法通常用于被重写。

handler.postDelayed vs. AlarmManager vs

ぐ巨炮叔叔 提交于 2020-01-01 19:27:09
问题 I have a minor problem in one of my apps. It uses a BroadCastReceiver to detect when a call finishes and then performs some minor housekeeping tasks. These have to be delayed for a few seconds, to allow the user to see some data and to ensure that the call log has been updated. I'm currently using handler.postDelayed() for this purpose: public class CallEndReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { if (DebugFlags.LOG

ABP框架 - 领域事件(EventBus)

隐身守侯 提交于 2020-01-01 18:37:07
文档目录 本节内容: EventBus 注入 IEventBus 获取默认实例 定义事件 预定义事件 处理完异常 实体修改 触发事件 处理事件 处理基类事件 处理程序异常 处理多个事件 处理程序注册 自动 手动 反注册 在C#里,一个类可以定义自己的事件,然后其它类可以注册它,当某些事情发生时,接收到通知。这对于桌面应用或单机的Windows服务非常有用。但是,对于一个Web应用,它就有点问题,因为对象在一个web请求里创建,并且它们生命周期都很短。所以就难于注册一些类事件,同时,直接注册另一个类的事件,也使得类之间更加藕合。 领域事件一般用来解藕业务逻辑和在应用里发生重要领域修改时发出通知。 EventBus EventBus是一个单例对象,被所有类触发事件或处理事件时共享。为使用事件总线,你先要引用它,有两种方式。 注入 IEventBus 你可以用 依赖注入 获取一个IEventBus的引用,这儿我们使用属性注入模式: public class TaskAppService : ApplicationService { public IEventBus EventBus { get; set; } public TaskAppService() { EventBus = NullEventBus.Instance; } } 在注入事件总线上,属性注入比构造器注入更合适

elasticsearch踩坑记录 no handler found for uri [http://localhost:9200/hyy/user/_search] and method [POST

不打扰是莪最后的温柔 提交于 2020-01-01 15:22:10
今天在尝试使用elasticasearch时碰到这个错误,在这里记录一下。 首先使用的是jest操作es,导入的pom为 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/io.searchbox/jest --> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> </dependency> 在配置文件里配置时发现spring.elasticsearch.jest.uris过时了,然后看到有host和port这两个配置,就用了上面的这种配置。这是导致后面出错的罪魁祸首。 在使用时直接注入 这个时候无论怎么操作都会报标题中的那个错。看错误信息也发现地址用的localhost,说明配置里面的那个没生效。 把配置文件注释掉 会发现又有用了。然后用过时的那个配置,发现也能成功。 说明如果直接注入JestClient的话,配置文件需要使用那种旧的配置方式,也就是过时的那种

MySQL transaction conundrum

六月ゝ 毕业季﹏ 提交于 2020-01-01 12:15:15
问题 I need to perform several inserts in a single atomic transaction. For example: start transaction; insert ... insert ... commit; However when MySQL encounters an error it aborts only the particular statement that caused the error. For example, if there is an error in the second insert statement the commit will still take place and the first insert statement will be recorded. Thus, when errors occur a MySQL transaction is not really a transaction. To overcome this problem I have used an error

Androids Handler.post, what happens exactly

倾然丶 夕夏残阳落幕 提交于 2020-01-01 09:28:28
问题 since several days, I tried to figure out what exactly happens if I execute code in void function(){ //somePreExecutionCode new Handler().post(new Runnable(){ @Override public void run(){ //someCode } }); } It seems like it isn't blocking the UI, so buttons, which calls function() doesn't stuck in the clicked position until someCode has finished. But if somePreExecutionCode starts a progressBar, the progressBar is shown at exactly the same moment, when someCode has finished. I know, there are

Python logging: propagate messages of level below current logger level

眉间皱痕 提交于 2020-01-01 09:27:54
问题 I want to log messages of a specific logger name, of a certain level and higher (say INFO and up) to a specific log handler, say a file handler, while still getting all log messages to the console. Python is version 2.7. What I tried until now was to create two loggers: A root logger A named logger For the root logger, I attached a logging.StreamHandler , and set the log level to logging.DEBUG . Then I attached a handler to the named logger and set level to logging.INFO for that logger. When

Best practice for eventbus with thread safety

拜拜、爱过 提交于 2020-01-01 06:11:31
问题 My app has activities for the user interaction and a background service which is the only place where the data model is being modified. The background service listens to actions that where made by the user as well as incoming messages from the network. Therefore concurrency issues can arise which I try to prevent by using a handler. For the event layer I use greenrobots Eventbus. This is all working well but I wonder if there is a smarter/faster/less code extensive (and therefore less error

How to manage Loopers and Threads (thread doesn't die anymore!)

天涯浪子 提交于 2020-01-01 04:54:06
问题 I created a class extending Thread to retrieve user location through LocationManager in a non-ui thread. I implemented this as a thread because it has to be started on request and do its work just for a limited time. By the way, I had to add a Looper object in the thread, to be able to create the handler for the LocationManager (onLocationChanged). This is the code: public class UserLocationThread extends Thread implements LocationListener { //... public void run() { try { Looper.prepare();