looper

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

爱⌒轻易说出口 提交于 2019-12-17 08:02:02
问题 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();

Handler+Looper+MessageQueue深入详解(根据源码)

霸气de小男生 提交于 2019-12-15 17:39:44
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Android应用程序都运行在一个dalvik虚拟机进程中,进程开始的时候会启动一个主线程(MainThread),主线程负责处理和ui相关的事件,因此主线程通常又叫UI线程。而由于Android采用UI单线程模型,所以只能在主线程中对UI元素进行操作, 简单的例子如下: package com.fangdo.android.ui; import org.slf4j.Logger; public class LoginActivity extends BaseActivity { Logger logger = LoggerFactory.getLogger(LoginActivity.class); /** Called when the activity is first created. */ private EditText phoneidET; private EditText passwordET; private Button logBT; private CheckBox savePasswordCB; private String phoneid; private String password; private Handler mHandler ; /** Called when the

Android中Message,MessageQueue,Looper,Handler详解+实例

↘锁芯ラ 提交于 2019-12-15 17:26:12
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、几个关键概念 1、MessageQueue:是一种 数据 结构,见名知义,就是一个消息队列,存放消息的地方。每一个线程最多只可以拥有一个MessageQueue数据结构。 创建一个线程的时候,并不会 自动 创建其MessageQueue。通常使用一个Looper对象对该线程的MessageQueue进行管理。主线程创建时,会创建一 个默认的Looper对象,而Looper对象的创建,将自动创建一个Message Queue。其他非主线程,不会自动创建Looper,要需要的时候,通过调 用prepare函数来实现。 2、Message:消息对象,Message Queue中的存放的对象。一个Message Queue中包含多个Message。 Message实例对象的取得,通常使用Message类里的静态方法obtain(),该方法有多个重载版本可供选择;它的创建并不一定是直接创建一个新的实例, 而是先从Message Pool(消息池)中看有没有可用的Message实例,存在则直接取出返回这个实例。如果Message Pool中没有可用的Message实例, 则才用给定的参数创建一个Message对象。调用removeMessages()时,将Message从Message Queue中删除

Handlers initialized with Looper.getMainLooper() does not respond to message callbacks

我只是一个虾纸丫 提交于 2019-12-13 13:12:19
问题 I am trying to implement Handlers listening on the same Looper from different threads. Below I have two Handlers, one created in the main thread, another in the child thread, however both are initialized to listen on the Main Looper. private Handler mMain; public static final ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMain

Explanation of handler, looper and related android thread classes [closed]

倖福魔咒の 提交于 2019-12-13 07:04:21
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . What are looper, handler and other terms related to android threads? How are these classes related? Where can I find detailed explanation of threads like timer, async task, handler, looper etc in android? 回答1: Here you go: Timer A facility for threads to schedule tasks for future

Can code execution can be interrupted by main event loop?

情到浓时终转凉″ 提交于 2019-12-12 05:24:27
问题 I am talking about one thread. For example I have an Activity ui and following methods in it: /* TOP LEVEL OF EXECUTION LOOPER NOW WORKING */ class MyActivity extends Activity { void onCreate(Bundle instance) { super.onCreate(instance); setContentView(R.layout.activity_main); doComplicatedStuff(); } void doComplicatedStuff() { // Doing stuf } void onClick() { // Process event } void anyOtherMethod() { /* NOT TOP LEVEL OF EXEUCTION, LOOPER NOW CAN'T WORK */ } } /* TOP LEVEL OF EXECUTION,

AsynchTask:Only one Looper may be created per thread

落爺英雄遲暮 提交于 2019-12-11 18:04:30
问题 My app crashing on every 4-5th call. It's showing me following exception. 10-14 14:00:30.651: E/AndroidRuntime(25035): FATAL EXCEPTION: AsyncTask #1 10-14 14:00:30.651: E/AndroidRuntime(25035): java.lang.RuntimeException: An error occured while executing doInBackground() 10-14 14:00:30.651: E/AndroidRuntime(25035): at android.os.AsyncTask$3.done(AsyncTask.java:299) 10-14 14:00:30.651: E/AndroidRuntime(25035): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 10-14 14:00

Android: requestLocationUpdates throws exception

二次信任 提交于 2019-12-11 06:46:57
问题 I'm trying to get periodically the user position via GPS in Android and send the data to a remote DB, but I get the exception: Can't create handler inside thread that has not called Looper.prepare() . The method that retrieves the position is in a remote service, and it's pretty basic: private void dumpLocationLog() { LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Looper.myLooper().prepare(); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,

using a Looper in a Service is the same as using a separate thread?

家住魔仙堡 提交于 2019-12-10 20:00:47
问题 In this example from the documentation (https://developer.android.com/guide/components/services.html#ExtendingService), we use the "looper" of a thread, and we use it in the Service class, and then the Service will be working as if it was in a separate thread? public class HelloService extends Service { private Looper mServiceLooper; private ServiceHandler mServiceHandler; // Handler that receives messages from the thread private final class ServiceHandler extends Handler { public

Looper.prepare() with AlertDialog

佐手、 提交于 2019-12-10 12:23:59
问题 I would like to insert a time counter inside a game. If the time is 0, there would be an AlertDialog which tells the user the time is out, and goes back to the previous Activity. Here is the method (it is inside a class which extends a SurfaceView): public void showTime(){ time--; Log.i("GameView time", "" + time); if (time <= 0){ Log.i("gameview time","time out"); gameTimer.setRunning(false); AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext()); AlertDialog alert = alt