android-handler

How the Looper knows to send the message to Handler?

橙三吉。 提交于 2019-12-21 17:28:43
问题 The question is, where I tell my Thread to use mHandler for the Looper ? Thank you. I am using the below code: class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } } 回答1: The question is, where I tell my Thread to use mHandler for the Looper? You don't need to tell it explicitly, because the system (framework) does it for you

Handler.handleMessage is not called in test, but is called in the app

荒凉一梦 提交于 2019-12-21 05:37:16
问题 I have a service that runs in a separate process. The service spawns a new thread in onCreate() method. This thread sends messages back to the service. If I start the app manually everything works fine - messages are received by the Handler in my service. But in my test handleMessage() method is never get called. How can I fix my test to make handleMessage() method work? Thanks! Service: public class MyService extends Service { private ServiceHandler mHandler; private final int MSG_HELLO

how can I call function every 10 sec?

ε祈祈猫儿з 提交于 2019-12-20 18:43:59
问题 I want make an app that call a function for example every 10 sec. I wrote my code like this: Handler ha=new Handler(); ha.postDelayed(new Runnable() { @Override public void run() { //call function } }, 10000); But my function call just one time in 10 sec after compile this code. How can I fix it? 回答1: Do it like this: final Handler ha=new Handler(); ha.postDelayed(new Runnable() { @Override public void run() { //call function ha.postDelayed(this, 10000); } }, 10000); 回答2: Use a combination of

GIF type animation for marker in google map api ANDROID

主宰稳场 提交于 2019-12-18 04:08:08
问题 I want to achieve a marker animation such as GIF animation. I got two images which should be blinking simultaneously. I found nothing which can acheive this in android. I am trying to do is , creating a handler which run every 1 second , and I am trying to set icon for marker. But it doesnt work. Please guide me in right direction. my code as of now is as follows. Handler handler = new Handler(); Boolean marker_color_bool = true; //adding marker and sending the marker instance to marker

GIF type animation for marker in google map api ANDROID

筅森魡賤 提交于 2019-12-18 04:08:01
问题 I want to achieve a marker animation such as GIF animation. I got two images which should be blinking simultaneously. I found nothing which can acheive this in android. I am trying to do is , creating a handler which run every 1 second , and I am trying to set icon for marker. But it doesnt work. Please guide me in right direction. my code as of now is as follows. Handler handler = new Handler(); Boolean marker_color_bool = true; //adding marker and sending the marker instance to marker

How to call a method in activity from a service

拥有回忆 提交于 2019-12-17 17:48:18
问题 There is a service that listens for some voice. If voice matches a string a certain method is invoked in the service object. public class SpeechActivationService extends Service { public static Intent makeStartServiceIntent(Context pContext){ return new Intent(pContext, SpeechActivationService.class); } //... public void onMatch(){ Log.d(TAG, "voice matches word"); } //... } This is how I start the service in my activity: Intent i = SpeechActivationService.makeStartServiceIntent(this);

Handler is abstract ,cannot be instantiated

风格不统一 提交于 2019-12-17 17:27:23
问题 I am trying to use a Handler in my app. But when i instantiate it like this: Handler handler = new Handler(); I get the following error. Gradle: error: Handler is abstract; cannot be instantiated And when i check the solutions, it asks me to implement these methods: Handler handler = new Handler() { @Override public void close() { } @Override public void flush() { } @Override public void publish(LogRecord record) { } }; I have never used Handlers before and i am using it just to call a method

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

情到浓时终转凉″ 提交于 2019-12-17 05:24:07
问题 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

Example communicating with HandlerThread

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

Update UI through Handler

我的未来我决定 提交于 2019-12-14 03:12:20
问题 I need to update my UI from an external Thread . I can't use runOnUiThread, because it's an application using StandOut libraries. So I created my Handler in the class containing methods to update the UI: private final class UIHandler extends Handler { public static final int DISPLAY_UI_TOAST = 0; private static final int LOAD_PROFILE = 1; public UIHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { switch (msg.what) { case UIHandler.DISPLAY_UI_TOAST: {