android-handler

android.os.Handler.removeMessages doesn't work when boxing value type as msg.obj

主宰稳场 提交于 2019-12-13 20:09:48
问题 (NOTE: I am re-writing my question because my first version was inaccurate). Given the following code: Handler handler = new Handler(); Message msg = handler.obtainMessage(12345, 67890L); handler.sendMessageDelayed(message, 10000); ... handler.removeMessages(12345, 67890L); The problem is that the message still fires 10 seconds after I sent it! Why isn't handler.removeMessages removing the message? 回答1: The problem is the auto-boxing of the long to a Long Object. In the debugger of

Android Handler actions not being processed

ⅰ亾dé卋堺 提交于 2019-12-13 05:59:33
问题 I am attempting to pass a message from a Thread to the Handler however, the Handler actions of the handler switch statement are never being processed. This is my Handler: private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch(msg.what) { case SUCCESS: ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj); String s = "Successfully Connected"; connectedThread

Restart a handler.postDelayed in Java [duplicate]

半腔热情 提交于 2019-12-13 03:23:13
问题 This question already has answers here : How to change/reset handler post delayed time? (2 answers) Closed last year . @Override public void onUserInteraction() { Handler handler = new Handler(); handler.postDelayed(new Runnable () { public void run() { finish(); } }, 5000); handler.removeCallbacks(null); } I am trying to run the handler when the OnCreate method is called. I would then like to cancel the handler and call it again (restart the handler) when I receive user interaction. How

Android: can you access AsyncTask class members in DoInBackground?

天大地大妈咪最大 提交于 2019-12-12 13:43:43
问题 Is it safe to run a method of an AsyncTask class member inside the DoInBackground ? or do you need to use a handler? private class MyAsyncTask extends AsyncTask<Void, Void, Void> { Object mObjA = null: private MyAsyncTask(Object objA) { mObjA = objA } protected void doInBackground(Void... params) { mObjA.aMethod() } } in doInBackground is it safe to run mObjA.aMethod() if mObjA wasn't passed as a parameter? is there a multithreading issue? should you in doInBackground only access objects

Inconsistent messaging between handler and runnable

此生再无相见时 提交于 2019-12-12 05:34:43
问题 I'm trying to update UI from a background thread through a Handler. Runnable sends periodically (minimum once every two seconds, maximum 16 times per second) new data to handler to be updated. On my runnable class I have a flag for starting/stoping loop -there are methods on main class attached to buttons for this task-. I have several problems with this: First message received is always 0 After a few loops, message received by handler is always 0 From time to time (sorry, poor precision, I

Calling SupportFragmentManager from PauseHandler implementation

对着背影说爱祢 提交于 2019-12-12 03:16:45
问题 I am trying to implement the PauseHandler described here: https://stackoverflow.com/a/8122789/1977132 My activity is an ActionBarActivity, the code in the processMessage method which brings up a dialog gives the error Cannot resolve method 'getSupportFragmentManager' Following the suggestion here: https://stackoverflow.com/a/27425568/1977132 I tried to change it to getFragmentManager() but then I get the error Cannot resolve method 'show(android.app.FragmentManager(), java.lang.String' I have

Android : check when listview finish implementation or ready

ⅰ亾dé卋堺 提交于 2019-12-12 02:48:04
问题 PROBLEM I want to click Listview Programmatically after onResume() and retain ListView but I don't know when and where should I implement click logic because now it Force close because I call function to early (I have check return view is null) I use Handler() Runnable() and Thread() method becuase I think that this will queue my code after listview finish implementation. What should I do?? EXAMPLE OF USING HANDLER RUNNABLE and THREAD final Handler handler = new Handler(); Runnable runnable =

Android: Run image classifier in infinite loop

泪湿孤枕 提交于 2019-12-11 15:40:41
问题 I want to run a image classifier in a infinite loop on a background thread. The function should be called immediately after launching the app. I want to feed the classifier with current frames from a prerecorded video which is simultaneously playing in the UI-thread, so the background thread should tell the UI-thread, once it's done, so I can feed it with the current frame and rerun the classifier. public class MainActivity extends AppCompatActivity { private static final String TAG =

Posting to Handler tied to current thread

折月煮酒 提交于 2019-12-11 13:34:40
问题 I have a Handler, mHandler , tied to the main thread. mHandler resides in a Service . Say I now post a Runnable to mHandler from the main thread like so: public class SomeService extends Service { // Handler is created on the main thread // (and hence it is tied to the main thread) private Handler mHandler = new Handler(); @Override public void onDestroy() { // onDestroy runs on the main thread // Is the code in this Runnable processed right away? mHandler.post(new Runnable() { // (Some code

Repeat a task with a time delay inside a custom view

扶醉桌前 提交于 2019-12-11 07:32:42
问题 The question Repeat a task with a time delay? talks about a repeated task within an activity. The top voted answer looks good for that situation. I am trying to make a blinking cursor inside a completely custom EditText. I tried copying and adapting code from the Android TextView and Editor code, but I wasn't getting anything to blink. Here is some of the current code I have been trying to get to work: private boolean shouldBlink() { if (!mCursorVisible || !isFocused()) return false; final