i am currently developing an auction app. each item will have a time frame before the auction ends on that item. i was wondering if anyone can give me an idea on how to listen to each item and check if the auction time has ended on that given item.
should i use a Service and build in it a trigger every second that checks each item in the database?
thank you in advance.
What I did for a previous app to solve a similar issue was to include the date/timestamp of the end date, and then implement a handler to implement a countdown until that date/time.
UpdateDisplay();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
}
private Runnable updateTimeTask = new Runnable() {
public void run() {
UpdateDisplay();
handler.postDelayed(this, 1000);
Log.i(TAG, "End of updateTimeTask");
}
};
And inside UpdateDisplay() I kept the code for updating the UI, based on some logic subtracting the current time from the time remaining to show a second hand ticking down.
Related
first of all, i already tried this: Continuously increase integer value as the button is pressed
But i had 59 errors, yep, 59, and as i used to use Eclipse which told you CLEARLY what kind of error you had, how to fix it, and Android Studio looks that was made for people with experience... I can't even understand what the hell to do, to fix all errors (btw, when i try to fix something i break 10 more somehow).
So... Given a Button and a TextView how do i do to increase the textview (like a Clicker game for example) and make it stop pressing the same button again:
And how do i put the intervals between each "click"
TextView score = (TextView) findViewById(R.id.textView);
score.setText(Integer.toString(i));
Button click = (Button) findViewById(R.id.button2);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
i++;
score.setText(Integer.toString(i));
}
});
By the way... I don't need the solution, i need to understand how exactly Thread or Handlers works, yes everybody will recommend me the Documentation, but i need to see a SIMPLE example explained part by part and i will understand way more than i already do by reading the documentation.
Given a Button and a TextView how do i do to increase the textview (like a Clicker game for example) and make it stop pressing the same button again: And how do i put the intervals between each "click"
Given your score and click widgets from your question:
Step #1: Add a Runnable field to your activity or fragment. Here, I'll call it incrementer.
Step #2: Define a static final int DELAY field in your activity or fragment, with the delay period you want ("intervals") in milliseconds.
Step #3: Have your Button use postDelayed() and removeCallbacks(), based on the state of incrementer:
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
if (incrementer==null) {
incrementer=new Runnable() {
#Override
public void run() {
i++;
score.setText(Integer.toString(i));
v.postDelayed(incrementer, DELAY);
}
};
incrementer.run();
}
else {
v.removeCallbacks(incrementer);
incrementer=null;
}
}
}
The incrementer field serves two roles. It tracks whether we are incrementing the TextView content or not, and it is the actual code that does the incrementing.
If incrementer is null, we are not presently incrementing the TextView. So, we assign incrementer a Runnable that can increment the TextView. The Runnable also calls postDelayed() to say "hey, run this Runnable again after DELAY milliseconds". We run() the Runnable ourselves the first time, to both populate the TextView at the outset and to trigger the postDelayed() call to schedule the next increment.
That will then continue to "loop" (run() calling postDelayed(), scheduling a future call to run()) until the user clicks the button again. Then, we see that incrementer is not null, so we must be incrementing the TextView and need to stop. removeCallbacks() unschedules the last postDelayed() call, stopping the "loop". We set incrementer to null mostly to prepare ourselves for the next button click.
I'm newbie to android I'm creating a quiz app . I need to make changes in quiz app to used for certain duration of time for users after that it should say session expired.
I'm creating a quiz app . opening the app will open main activity which takes the user id and compares with the one in array. If it is true . it proceeds to next activity c1Activity in which question will be asked and user need to enter answer which is equated with stored value from array . if it is correct the user is proceeded to next question . similarly it go on until last question . my need is that the user should answer all questions within 3minutes if he lags the app should open a activity which shows times up
The most simple solution I can think for this is to implement a class that extends CountDownTimer.
First, you implement the class:
class MyTimer extends CountDownTimer{
// first param is the total amount of time it will countdown
// second param is to fire onTick() method, in case you need to fire an alert or something
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onTick(long millisUntilFinished) {
//You can fire a Toast from here indicating how much time is left
}
public void onFinish() {
//You can start the new activity from here
}
}
Finally, you just have to create and start an object of this class:
//3 minutes = 3 * 60 * 1000
new MyTimer(180000, 1000).start();
I am developing an app. The basic concept of the app is that I have to keep few video playing continuously. The list of videos i.e playist is maintained by webservice. The playlist has dates on which date which playlist is to be played. My problem is how to check the date and load playist accordingly? The videos from proper playlist should keep on playing. So do I need to keep a service in background which checks the date each day or what do i need to do?
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
webservicecall();
}
}, 2000); // 2sec
this is an eg. to set time interval u can use it logically as u want
Use a specific function which gets called by itself after your required time and check the time and date condition if matches do the task..
if u wish a delay/ recursive call function can be provided..
In my application i need to decrypt the certain message format, to extract information like message id, timeout and so on.i need to show an corresponding image for the given id as well as to show it for the mentioned time period.
For that i have created one custom layout to show the image and other details. i'm using imageview for displaying the image. but dont know how to set timeiut for that?
Do anyone have idea on that?
You can easily use Handler to do that, like this
imageuser.setImageBitmap(bitmapObject);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imageuser.setImageBitmap(null);
}
}, 5000);
here, imageuser is your ImageView and replace 5000 with your specific time in miliseconds.
Just use it like whenever you want to show image just call your UI and start a thread for the given time you want to show the image and when the time complete just make that ui visibility gone,this is the logic try it in your own way.
thanks
I have a linear layout in which I am placing a textview with the id "out". In my code i am getting this textview and calling out.append("Some string here");. What i want to do is have it use a thread.sleep(1000); to wait one second and then do another append. When i just use a for loop and iterate through it ten times it waits ten seconds and then updates the view at the end. How can i make this update the view in between the sleeps?
ps. The main reason for this is because i have another thread running with a bluetooth output stream and i want it to update the textview every time i send a byte to an arduino connected through a bluesmirf module. I can get it to send data but the updating of the screen happens at the end of the for loop. If i put a sleep in this loop it will wait the one second and then output to the arduino no problem. I just want to update it so i can see where things fail as they fail without using the logs.
Maybe you can use a postdelay handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//Here is the code you want to run after 1 second
}
}, 1000);
The handler is not blocking, but maybe it can help
Excuse me for my bad english, good luck!