android-handler

Android: When should I use a Handler() and when should I use a Thread?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 21:18:06
When I need something to run asynchronously , such as a long running task or a logic that uses the network, or for whatever reason, Starting a new Thread and running it works fine. Creating a Handler and running it works as well. What's the difference? When should I use each one? What are the advantages / reasons to use a Handler and not a Thread ? PS. - For this question's sake, let's ignore AsyncTask . - Handler().postDelayed use case is clear to me, for this question's sake let's assume I need the task to start immediately. FoamyGuy If whatever you are doing is "heavy" you should be doing

How to remove all callbacks from a Handler?

╄→гoц情女王★ 提交于 2019-11-26 19:29:37
I have a Handler from my sub-Activity that was called by the main Activity . This Handler is used by sub-classes to postDelay some Runnables, and I can't manage them. Now, in the onStop event, I need to remove them before finishing the Activity (somehow I called finish() , but it still call again and again). Is there anyway to remove all callbacks from a Handler? josh527 In my experience calling this worked great! handler.removeCallbacksAndMessages(null); In the docs for removeCallbacksAndMessages it says... Remove any pending posts of callbacks and sent messages whose obj is token. If token

Handlers, MessageQueue, Looper, do they all run on the UI thread?

。_饼干妹妹 提交于 2019-11-26 12:18:12
问题 I\'m trying to wrap my head around threading, and I know that I may use a Handler to post messages/runnables to the MessageQueue , which in turn gets picked up by the Looper and sent back to the Handler for processing. If I post to a handler in my activity, is the Activity , Handler , MessageQueue and Looper all running on the UI thread? If not, could someone please explain how this all comes together? :) 回答1: Short answer: they all run on the same thread. If instantiated from an Activity

AsyncTaskLoader basic example. (Android)

允我心安 提交于 2019-11-26 12:14:13
问题 I am using a Loader in my application and based on the result I get from the query I perform on COntacts using this Loader I perform some calculations and store them back in a Sqlite DB. I want this operation to be Asynchronous, however I am confused between using an Async task, as I have lot of different data types to return or should I use a simple handler or an AsyncTaskLoader, I want it to be simple as I am new to Loaders. I tried to search around for examples of AsyncTaskLoader but it

Example communicating with HandlerThread

丶灬走出姿态 提交于 2019-11-26 12:08:11
I want to set up a HandlerThread from the GUI thread. Then some time later, when a button is clicked on the GUI, it runs callHello(), which then send a message to a HelloLogger object residing on the non-GUI thread which asynchronously logs "Hello World". I have tried a number of things, some block indefinitely, some never receive the message, etc etc. The code below is more or less as close as I have got, please could someone modify it to work? public class HandlerThreadExample { private MyHandlerThread mMyHandlerThread; private Looper mLooper; private Handler mHandler; public

How to update ui from asynctask

喜欢而已 提交于 2019-11-26 11:17:33
问题 I\'ve seen many examples of how to do this, but I can\'t figure how to implement it in my code. I am using this code. I have updated the url, so it will receive a json with dynamic data. What I\'m trying to do is to automatically update the list every 30 secs with this code. Handler handler = new Handler(); Runnable refresh = new Runnable() { public void run() { new GetContacts().execute(); handler.postDelayed(refresh, 30000); } }; It refreshes, and calls the url and gets the data, but the UI

Running code in main thread from another thread

白昼怎懂夜的黑 提交于 2019-11-26 08:50:12
问题 In an android service I have created thread(s) for doing some background task. I have a situation where a thread needs to post certain task on main thread\'s message queue, for example a Runnable . Is there a way to get Handler of the main thread and post Message / Runnable to it from my other thread ? Thanks, 回答1: NOTE: This answer has gotten so much attention, that I need to update it. Since the original answer was posted, the comment from @dzeikei has gotten almost as much attention as the

cancelling a handler.postdelayed process

人走茶凉 提交于 2019-11-26 08:47:24
问题 I am using handler.postDelayed() to create a waiting period before the next stage of my app takes place. During the wait period I am displaying a dialog with progress bar and cancel button. My problem is I can\'t find a way to cancel the postDelayed task before the time elapses. 回答1: I do this to post a delayed runnable: myHandler.postDelayed(myRunnable, SPLASH_DISPLAY_LENGTH); And this to remove it: myHandler.removeCallbacks(myRunnable); 回答2: In case you do have multiple inner/anonymous

How to remove all callbacks from a Handler?

假装没事ソ 提交于 2019-11-26 06:58:12
问题 I have a Handler from my sub-Activity that was called by the main Activity. This Handler is used by sub-classes to postDelay some Runnables, and I can\'t manage them. Now, in the onStop event, I need to remove them before finishing the Activity (somehow I called finish() , but it still call again and again). Is there anyway to remove all callbacks from a Handler? 回答1: In my experience calling this worked great! handler.removeCallbacksAndMessages(null); In the docs for

Handler vs AsyncTask vs Thread [closed]

半世苍凉 提交于 2019-11-25 23:02:48
问题 I got slightly confused about the differences between Handlers , AsyncTask and Threads in Android. I\'ve read quite a few blogs and questions here in StackOverflow. Handler are background threads that provide you to communicate with the UI. Updating a progress bar, for instance, should be done via Handler . Using Handlers you have the advantage of MessagingQueues , so if you want to schedule messages or update multiple UI elements or have repeating tasks. AsyncTask are similar, in fact, they