postdelayed

How to execute a function every two seconds in Java on Android?

余生长醉 提交于 2019-11-28 01:24:46
问题 I'm trying to execute a chunk of Java code in my Android program every two seconds. My code currently looks like this: LinearLayout.postDelayed(new Runnable() { public void run() { //Do stuff here } }, 2000); Unfortunately, this only runs once after two seconds and then never runs again. How could I get it to run every two seconds? Thanks in advance for all your help. 回答1: Try this: LinearLayout.postDelayed(new Runnable() { public void run() { //Do stuff here // assuming LinearLayout is

How to get an Android widget's size after layout is calculated?

我是研究僧i 提交于 2019-11-27 15:32:17
I have a layout which specifies sizes of widgets in relative dimension, for example: <LinearLayout ... layout_height="fill_parent"> <ImageView ... layout_height="wrap_content" /> <TextView ... layout_height="120dp" /> </LinearLayout> Immediately after onCreate , I want to know how much is the height of the ImageView. How to do that? Note: If I call getHeight() in onCreate , I get 0. I have also tried imageView.postDelayed , it works on 2.1 emulator but fails on 1.5 emulator (I got 0, too). Finally, I tried to create a Handler , and then I call handler.postDelayed with 10 ms delay. It works in

How to remove a runnable from a handler object added by postDelayed?

会有一股神秘感。 提交于 2019-11-27 10:24:45
问题 I have an "open" animation and am using Handler.postDelayed(Runnable, delay) to trigger a "close" animation after a short delay. However, during the time between open and close, there is possibly another animation triggered by a click. My question is, how would I cancel the "close" animation in the handler? 回答1: Just use the removeCallbacks(Runnable r) method. 回答2: Cristian's answer is correct, but as opposed to what is stated in the answer's comments, you actually can remove callbacks for

nested postDelayed / Runnable / Handler Android

六眼飞鱼酱① 提交于 2019-11-27 06:26:26
问题 I am trying to use a nested postDelayed because I need to do something after (delayed for) 5 minutes, stop it after (delayed) 30 seconds, do something else, then repeat both events in the cycle again from the start. I just can't seem to get it right. code I have sofar: private long EnabledAfter = 300000; // 5 minutes private long DisabledAfter = 30000; // 30 seconds public void start_timers(){ on_delayed(EnabledAfter); }//end method private void on_delayed(long period_off){ Delayed = new

how to use postDelayed() correctly in android studio?

依然范特西╮ 提交于 2019-11-27 04:27:59
问题 I have a countDownTimer and if the user does not hit the gameButton within the 12th second I want the gameOver method called. problem I either get game function called instantly when countDownTimer is 12 or the timer just keeps counting down. So I am trying to use the postDelayed() method to give the user a full second to hit the button and let the countDownTimer to continue but as my code is right now the game stops on 12 regardless. import android.app.Activity; import android.os

Stop handler.postDelayed()

不问归期 提交于 2019-11-27 03:24:56
I call multiple Handlers by new Handler().postDelayed(new Runnable()..... How can I stop it, if I click on return button? public class Tag1 extends Oberklasse implements OnClickListener { public Button btn; //private Handler myHandler = new Handler(); Handler handler; Runnable myRunnable; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tt1); //Bildschirm soll anbleiben!!! getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //Lautstärke BUttons enable setVolumeControlStream(AudioManager.STREAM_MUSIC);

How to get an Android widget's size after layout is calculated?

北战南征 提交于 2019-11-26 22:25:40
问题 I have a layout which specifies sizes of widgets in relative dimension, for example: <LinearLayout ... layout_height="fill_parent"> <ImageView ... layout_height="wrap_content" /> <TextView ... layout_height="120dp" /> </LinearLayout> Immediately after onCreate , I want to know how much is the height of the ImageView. How to do that? Note: If I call getHeight() in onCreate , I get 0. I have also tried imageView.postDelayed , it works on 2.1 emulator but fails on 1.5 emulator (I got 0, too).

Android - running a method periodically using postDelayed() call

时间秒杀一切 提交于 2019-11-26 20:00:01
I have a situation in an Android app where I want to start a network activity (sending out some data) which should run every second. I achieve this as follows: In the onCreate() I have the code: tv = new TextView(this); tv.postDelayed(sendData, 1000); The sendData() function: Handler handler = new Handler(); private Runnable sendData=new Runnable(){ public void run(){ try { //prepare and send the data here.. handler.removeCallbacks(sendData); handler.postDelayed(sendData, 1000); } catch (Exception e) { e.printStackTrace(); } } }; The problem come in like this: When user presses the back

Android - running a method periodically using postDelayed() call

强颜欢笑 提交于 2019-11-26 08:59:51
问题 I have a situation in an Android app where I want to start a network activity (sending out some data) which should run every second. I achieve this as follows: In the onCreate() I have the code: tv = new TextView(this); tv.postDelayed(sendData, 1000); The sendData() function: Handler handler = new Handler(); private Runnable sendData=new Runnable(){ public void run(){ try { //prepare and send the data here.. handler.removeCallbacks(sendData); handler.postDelayed(sendData, 1000); } catch

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