I want to make an application about mini game.
Detail : In 2 seconds you must to answer a question if you don't answer or the answer is wrong -> Game Over . But if your answer is true the Timer will reset become 0 and countdown again with diffirent question.
I have already seen many code about timer in website but I don't understand clearly about it :(
So I want to ask : How can i set up a timer run only 2 seconds and how can i reset it and continue with a new question ?
Please help me.
you can use CountDownTimer in android like this:
public class Myclass {
myTimer timer =new myTimer(2000,1000);
public void creatQuestion(){
timer.start();
//method you init question and show it to user
}
public void getUserAnswer(/*evry thing you expected*/)
{
//if answer is true call timer.start()
//else call timer.onFinish(); to run onfinish in timer
}
public class myTimer extends CountDownTimer {
public myTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
// you can update ui here
}
#Override
public void onFinish() {
this.cancel();
//fire game over event
}
}
}
i hope it make you satisfy
I've done something similar using Thread/Runnable.
Thread t = new Thread(new Runnable() {
public void run() {
final long startTime = getTime();
final long maxEndTime = startTime + 2000L;
try {
while (shouldContinueWaiting()) {
if (getTime() > maxEndTime) {
throw new TimeoutException();
}
sleep();
}
} catch (InterruptedException e) {
handleInterrupt();
} catch (TimeoutException e) {
handleTimeout();
}
}
boolean shouldContinueWaiting() {
// Has the user already answered?
}
void handleInterrupt() {
// The user has answered. Dispose of this thread.
}
void handleTimeout() {
// User didn't answer in time
}
void sleep() throws InterruptedException {
Thread.sleep(SLEEP_DURATION_IN_MILLIS);
}
void getTime() {
return System.currentTimeMillis();
}
then you can start/restart the thread by:
t = new Thread(same as above...);
t.start();
and stop by:
t.interrupt();
We want to use the Timer class.
private Timer timer;
When you're ready for the timer to start counting -- let's say it's after you press a certain button -- do this to start it:
timer = new Timer();
timer.scheduleAtFixedRate(incrementTime(), 0, 100);
The first line is us creating a new Timer. Pretty standard. The second line, however, is the one I wanted you to see.
incrementTime() is a method that is called at the end of every "tick" of the clock. This method can be called whatever you want, but it has to return an instance of TimerTask. You could even make an anonymous interface if you want, but I prefer moving it off into its own section of code.
The 0 is our starting location. We start counting from here. Simple.
The 100 is how large a "tick" of the clock is (in milliseconds). Here, it's every 100 milliseconds, or every 1/10 of a second. I used this value at the time of writing this code because I was making a stopwatch application and I wanted my clock to change every 0.1 seconds.
As for your project, I'd suggest making the timer's task be your question switch method. Make it happen every 2000 milliseconds, or 2 seconds.
You can use a Handler.
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
//this will happen after 2000 ms
}
}, 2000);
Maybe this can help you:
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
// FIRE GAME OVER
handler.postDelayed(this, 2000); // set time here to refresh textView
}
});
You can fire your game over after 2000 milliseconds.
If you get the question correct -> remove callback from handler and reset it when the next question starts.
Related
I want to set network status in TextView, which I want to repetitively call method and set in background, so I used AsyncTask class with infinite loop
class setNetworkText extends AsyncTask
{
#Override
protected Object doInBackground(Object[] params) {
for(;;)
{
if(isNetworkConnected()) //check internet connection and if found it return true
setOnline(); // it set my TextView text to Online
else
setOffline(); // it set my TextView text to Offline
Thread.sleep(2000);
}
return null;
}
}
but it is not working, it stops my application.
Android will (in most versions) only execute one AsyncTask at a time - so if you keep blocking in doInBackground of one AsyncTask, no other AsyncTasks will run, thus blocking your application.
Take a look at using Handler.postDelayed or using a TimerTask. They are more suited for repeating actions.
You can not use AsyncTask to do that. You should use Handler to schedule a task periodically.
// Create the Handler
Handler handler = new Handler();
// Define the code block to be executed
private Runnable runnableTask = new Runnable() {
#Override
public void run() {
if(isNetworkConnected())
setOnline();
else
setOffline();
}
};
// Call on main thread (for example, inside onResume())
#Override
protected void onResume() {
super.onResume();
handler.postDelayed(runnableTask, 2000);
}
// Remember to unregister it onPause()
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(runnableTask);
}
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//check something on time interval here 1 second
}
public void onFinish() {
//when your task done here 3 second is time to end
}
}.start();
explanation
CountDownTimer(long millisInFuture, long countDownInterval)
millisInfuture will be how long you want to run the task and countDownInterval is the interval in your case it is 2 seconds
I'm having an issue with my CountDownTimer. Here's the situation:
Working on a game and I am not using a framework (who needs one, right? coughs) and I have a CountDownTimer running. This CountDownTimer is used to calculate how long a player has to complete a level, and also update the screen per 100ms tick.
In the first level this works fine, but in the second level it ticks per 50ms, and in the third per 60/90/50ms ticks (yes, in that order repeatedly) making the game behave odly and messing up collision.
I have no idea what's going on here.
I've ruled out if it's dependant on the level by loading later levels first, and upon solving those the problem arrises all the same.
I have, however, pinpointed where it's going wrong: During the loading for a new level, and only then; even reloading a current level doesn't bring this bug up.
I have a suspicion that somehwere in this code, something's not going as it should be:
public void prepareNewLevel(){
RelativeLayout rl = (RelativeLayout)findViewById(R.id.game);
level.clear();
movables.clear();
img_move.clear();
img_noMove.clear();
timerCount.cancel();
totalSolved = 0;
solvable = 0;
levelWidth = 0;
levelHeight = 0;
allCollided = false;
firstDraw = true;
rl.removeAllViewsInLayout();
}
level, movables, img_move and img_noMove are List items.
This my timer:
MyCount update = new MyCount(15*1000, 100);
This is MyCount:
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
remaining_time = millisUntilFinished;
if(!loadingNewLevel)
MoveObjects();
}
#Override
public void onFinish() {
resetLevel = true;
upMotion = false;
downMotion = false;
leftMotion = false;
rightMotion = false;
update.start();
}
}
And then I also call update.start() at the end of my loadLevel() function. I first thought I had two timers running at once, but writing to the LogCat showed me that was false, concidering I got a neat 'reset' message every 15 seconds, instead of at multiple moments smaller than 15s.
And that's basically the issue here. Now that I know where it's being caused and what the issue is, how do I solve it?
-Zubaja
Try use Timer instead CountDownTimer:
// start timer inside your method
if( timer != null) timer.cancel();
timer = new Timer();
timer.scheduleAtFixedRate( new TimerTask()
{
#Override
public void run()
{
handler.obtainMessage( yourInegerMessage).sendToTarget();
}
}, 100, 15000);
// and define you handler
public Handler handler = new Handler( new Handler.Callback()
{
#Override
public boolean handleMessage( Message message)
{
// use 'message.what' as yourInegerMessage
// ...
return true;
}
});
// and stop your timer after first time
May be this help you.
May be you start more one CountDownTimer? Try stop existing CountDownTimer before start new CountDownTimer.
I am trying to develop a game like matching small pictures.My problem is that i want to finish the game after a time period.For instance in level 1 we have 10 seconds to match picture.I want to display remaining time also.I will be thankful for any help.
Since you also want to show the countdown, I would recommend a CountDownTimer. This has methods to take action at each "tick" which can be an interval you set in the constructor. And it's methods run on the UI Thread so you can easily update a TextView, etc...
In it's onFinish() method you can call finish() for your Activity or do any other appropriate action.
See this answer for an example
Edit with more clear example
Here I have an inner-class which extends CountDownTimer
#Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.some_xml);
// initialize Views, Animation, etc...
// Initialize the CountDownClass
timer = new MyCountDown(11000, 1000);
}
// inner class
private class MyCountDown extends CountDownTimer
{
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
frameAnimation.start();
start();
}
#Override
public void onFinish() {
secs = 10;
// I have an Intent you might not need one
startActivity(intent);
YourActivity.this.finish();
}
#Override
public void onTick(long duration) {
cd.setText(String.valueOf(secs));
secs = secs - 1;
}
}
#nKn described it pretty well.
However, if you do not want to mess around with Handler. You always can delay the progress of the system code by writing:
Thread.sleep(time_at_mili_seconds);
You probably need to surround it with try-catch, which you can easily add via Source-> Surround with --> Try & catch.
You can use postDelayed() method of Handler...pass a Thread and specific time after which time the Thread will be executed as below...
private Handler mTimerHandler = new Handler();
private Runnable mTimerExecutor = new Runnable() {
#Override
public void run() {
//here, write your code
}
};
Then call postDelayed() method of Handler to execute after your specified time as below...
mTimerHandler.postDelayed(mTimerExecutor, 10000);
While there must be a better tactic of completing a countdown I have set up in my application, it seemed clear to me that using Thread.sleep(1000) between every digit would work. It did not and I still do not have any other solution. When I run the app, the countdown goes from 5, straight to 1 in 1 second.
This is my code:
bBegin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bBegin.setText("5");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
}
bBegin.setText("4");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
}
bBegin.setText("3");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
}
bBegin.setText("2");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
}
bBegin.setText("1");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
}
}
Thanks, I hope this problems helps other noobs like me (and no worries, I did check the site for a previous solution)
Thread.sleep(1000); Bad! You are calling sleep() on the UI thread which you alsmost, if ever, never want to do. The UI will sleep for that amount of time. Use CountDownTimer instead.
CountDownTimer Docs
There are certainly other ways but this one should be good to accomplish what you want.
Example
In your case you might use a CountDownTimer something like this.
Create an inner class
private class MyCountDown extends CountDownTimer
{
long duration, interval;
public MyCountDown(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
duration = millisInFuture;
interval = countDownInterval;
int secs = 5; // varaible for counter to be placed in TextView
start();
}
#Override
public void onTick(long duration)
{
bBegin.setText(String.valueOf(secs));
secs = secs - 1;
}
#Override
public void onFinish() {
secs = 5; // reset counter if needed or do anything else you need when countdown is over
}
And call it in onClick() like
MyCountDown timer = new MyCountDown(5000, 1000); // local variable but might want to make it a member variable
Sleep doesn't "skip" anything - instead, this code prevents the UI from updating until after the method ends (after it sleeps all 5 times).
At some point after the onClick method ends - and the UI dispatch can resume operation - the UI is updated. When this update occurs the display is updated with the last value assigned, which leads to the incorrect conclusion that code was "skipped". However, all of the code in the method ran.
This leads us to a very important rule: do not sleep on the main-UI thread. Doing so blocks user interaction and it blocks display updates because it blocks the UI event/dispatch queue. All standard UI callbacks, such as click listeners, occur on the UI thread.
Where sleep is required - as in the case of games and custom canvases, say - the sleep is done on a different (non UI) thread so that it does not cause such primary UI blocking behavior.
**As in the attached code snippet it is showing user thread is trying to update UI Thread.which is not a good practice..
i have done same using the concept of Handler.
the code snipper is ..**
public class MainActivity extends Activity {
TextView tv;
Button b1;
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView1);
b1=(Button) findViewById(R.id.button1);
handler= new Handler();
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Runnable r=new Runnable() {
int mynumber=5;
#Override
public void run() {
while(mynumber >0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
tv.setText("remaining time is "+mynumber );
}
});
mynumber--;
}
}
};
new Thread(r).start();
}
});
}
}
I have this method
public void GetSMS(){
//in this method I read SMS in my app inbox,
//If have new SMS create notification
}
for this I think create timer tick method and every 5 sec call GetSMS()
How can I create a correct method for that ?
Here is an example of Timer and Timer Task. Hope this helps.
final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
// Do whatever you want
}
});
}
};
timer.schedule(timerTask, 1000); // 1000 = 1 second.
Maybe with a timer and a timertask?
See javadocs:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html
Yet receiving broadcasts is probably a more solid solution.
See: Android - SMS Broadcast receiver
Use Timer.scheduleAtFixedRate() as follow:
final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
GetSMS();
}
});
}
};
timer.scheduleAtFixedRate(timerTask, 5000, 5000); // every 5 seconds.
I saw it by accident.. This is not the right way to do it..
You don't need to check if there is a sms that received. Android provide broadcast receiver to get notified when sms is income.
Here you go, you have the link here.. Copy paste and it will work great
http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87
Hope that this make sense
Although the above timer methods are the correct way to use timers of the sort you are after, I quite like this little hack:
new CountDownTimer(Long.MAX_VALUE, 5000)
{
public void onTick(long millisUntilFinished)
{
// do something every 5 seconds...
}
public void onFinish()
{
// finish off when we're all dead !
}
}.start();
Long.MAX_VALUE has, according the Java docs, a (signed) value of 2^63-1, which is around 292471 millennia ! So starting up one of these countdown timers effectively lasts forever relatively speaking. Of course this depends on your interval time. If you want a timer every 1 second the timer would "only" last 58494 millenia, but we don't need to worry about that in the grander scheme of things.