android-handler

Will a Handler postDelayed not being fired when CPU sleeps?

♀尐吖头ヾ 提交于 2019-12-03 16:54:13
问题 I have an activity with some Handlers that are being executed at intervals no more than 5 minutes. The activity is launched from a BroadcastReceiver and may be launched with the screen off waiting for the user to grab the phone and get the user input, when this happens, the activity onPause() is called so for sure CPU is in sleep mode. I have not set the activity to turn screen on because I want to save as much battery as possible. I've tested it with my phone and works really well, while

Why use AsyncTaskLoader with LoaderManager, instead of simple Handler?

孤人 提交于 2019-12-03 05:19:46
问题 Running asynchronous tasks off of the UI thread then modifying the UI is a common issue in android development, so I decided to take some time, research, and play around with different techniques and find what works best for me. What I considered important factors: Should work reliably Code readability Activity or Fragment should be kept clean of as much of thread management as possible Here is the summary of my impressions (which may be wrong and some are just opinions) about the various

Will a Handler postDelayed not being fired when CPU sleeps?

南楼画角 提交于 2019-12-03 05:11:26
I have an activity with some Handlers that are being executed at intervals no more than 5 minutes. The activity is launched from a BroadcastReceiver and may be launched with the screen off waiting for the user to grab the phone and get the user input, when this happens, the activity onPause() is called so for sure CPU is in sleep mode. I have not set the activity to turn screen on because I want to save as much battery as possible. I've tested it with my phone and works really well, while screen is off all Handlers execute all the code they have to run. If I turn the screen on and off while

Change View from other thread

半城伤御伤魂 提交于 2019-12-02 11:02:51
问题 I wrote a code to download an image from internet. And i have to show it in a ImageView which is dynamically created. And i am getting an error that Only the original thread that created a view hierarchy can touch its views. I know i have to write a handle but how can i do that? Here is my code: public class ResimCek implements Runnable { int resimID = 0; public ResimCek(int parcaID) { // store parameter for later user resimID = parcaID; } public void run() { int resID = getResources()

handler.postDelayed is not working in onHandleIntent method of IntentService

隐身守侯 提交于 2019-12-01 17:52:21
final Handler handler = new Handler(); LOG.d("delay"); handler.postDelayed(new Runnable() { @Override public void run() { LOG.d("notify!"); //calling some methods here } }, 2000); The "delay" does shows in the log, but not others at all. And the method called in the run() is not called at all also. Can anyone help explain why this happens, am I doing anything wrong? The class that has this code extends IntentService, will this be a problem? ============================ UPDATE: I put this code in the class that extends IntentService . The only place I found it worked was in the constructor. But

What happens to this thread runnable at the end once it is completed?

送分小仙女□ 提交于 2019-12-01 17:38:18
I have this thread which downloads a few images from the server. So once it downloads the images I call the handler and carry on UI updation. So since stop() for thread is deprecated I am not able to use it. I have two questions here. What happens to this thread in the end?(means after I call the handler method what happens to it). OR how do I stop this thread without using stop()? Here is my code. handler=new Handler() { public void handleMessage(Message msg) { if(msg.what==0) { //UI Updation takes place. } } }; final Thread t = new Thread(new Runnable() { public void run() { Log.i("Inside

handler.postDelayed is not working in onHandleIntent method of IntentService

試著忘記壹切 提交于 2019-12-01 16:38:40
问题 final Handler handler = new Handler(); LOG.d("delay"); handler.postDelayed(new Runnable() { @Override public void run() { LOG.d("notify!"); //calling some methods here } }, 2000); The "delay" does shows in the log, but not others at all. And the method called in the run() is not called at all also. Can anyone help explain why this happens, am I doing anything wrong? The class that has this code extends IntentService, will this be a problem? ============================ UPDATE: I put this code

Handler.postDelayed(Runnable) vs CountdownTimer

♀尐吖头ヾ 提交于 2019-12-01 15:03:54
问题 Sometimes we need to delay a code before it runs. This is doable by the Handler.postDelayed(Runnable) or CountdownTimer . Which one is better in terms of performance? See the sample code below Handler new Handler().postDelayed(new Runnable() { @Override public void run() { //DO SOMETHING } }, 1000); CountDownTimer new CountDownTimer(1000, 1000) { public void onFinish() { //DO SOMETHING } public void onTick(long millisUntilFinished) {} }.start(); 回答1: The Handler should offer you better

Android: Create a background thread that runs periodically and does UI tasks?

对着背影说爱祢 提交于 2019-12-01 11:22:58
OK, so I know how to do a backround task, I know how to do a periodic task (using handle postdelayed and runnable), I also know how to do UI task from background thread (via handler) but I am not able to execute a periodic background task that does some action on the UI thread. I am trying to execute some background task every minute in which I have to make a network call. After the call is over, depending on the output I have to update the UI. I tried to do something like this private void DoTask() { Thread thread = new Thread() { public void run() { Looper.prepare(); final Handler handler =

Android: Create a background thread that runs periodically and does UI tasks?

倖福魔咒の 提交于 2019-12-01 09:43:20
问题 OK, so I know how to do a backround task, I know how to do a periodic task (using handle postdelayed and runnable), I also know how to do UI task from background thread (via handler) but I am not able to execute a periodic background task that does some action on the UI thread. I am trying to execute some background task every minute in which I have to make a network call. After the call is over, depending on the output I have to update the UI. I tried to do something like this private void