i need to include timer in my application.i just gone through the example given in the sites.and tried to implement that in my program.but the thing is i dont know how to work with timer.in my program i want to start the timer on a button click.timer has to show in the screen.but the screen is a canvas(when we click on button it loads a canvas) with moving objects.when two objects collide timer has to stop and has to show the value in a message box.given below is my main activity code.
public class MyActivity extends Activity {
private static final String TAG = MyActivity.class.getSimpleName();
public static Timer myTimer;
private Button startbtn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
startbtn=(Button)findViewById(R.id.startbtn);
startbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set our MainGamePanel as the View
setContentView(new MainGamePanel(getApplicationContext()));
Log.d(TAG, "View added");
}
});
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
#Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}
#Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
}
};
}
i know how to check collision.i need only timer part like starting timer,showing on the screen and stopping it from another class.can anybody help me...plz....
Use this the code will be repeated after one second and timer will run for 30sec
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
finish();
}
}.start();
Related
I've implemented a CountDownTimer in my code as follows: At the top of the class, I create
CountDownTimer myTimer;
Then when a user presses button Start, the following method is called:
private void countme()
{
final int tick = 500;
final int countTime = 10000;
myTimer = new CountDownTimer(countTime, tick) {
#Override
public void onTick(final long millisUntilFinished) { }
#Override
public void onFinish() {
myPicture.setVisibility(View.GONE);
}
};
myTimer.start();
}
I have button Stop all myTimer.cancel(). As you can see, if the timer is not cancelled, myPicture will disappear.
Even if I click the stop button so that myTimer.cancel() is called (I checked this with log statements), the counter still continues to count down and to make the picture disappear when it's done.
Why isn't it stopping? How do I get it to actually cancel?
To clarify, I do know how to implement Runnable timers, but they are not as accurate for my needs as CountDownTimers are, which is why I'm not using them in this case.
After a lot of tries, trick is to declare the timer in onCreate but start and cancel it in some other method. The onFinish() will not call after cancelling the timer.
myTimer = new CountDownTimer(COUNT_DOWN_TIME, TICK) {
#Override
public void onTick(final long millisUntilFinished) {
((TextView) findViewById(R.id.textView3)).setText(""
+ millisUntilFinished);
}
#Override
public void onFinish() {
findViewById(R.id.timer_imageBiew).setVisibility(View.GONE);
}
};
private fun startTimer() {
myTimer .start()
}
private fun stopTimer() {
myTimer .cancel()
}
Here in your method countme() you are initializing myTimer, so outside this method myTimer has no value.
Use this
Declare at the top
CountDownTimer myTimer;
final int tick = 500;
final int countTime = 10000;
In the onCreate method of Activity or Fragment
myTimer = new CountDownTimer(countTime, tick) {
#Override
public void onTick(final long millisUntilFinished) { }
#Override
public void onFinish() {
myPicture.setVisibility(View.GONE);
}
};
Now use myTimer.start() to start and myTimer.cancel() to stop it.
Hope you understood.
Your post is very odd. I just tried doing a sample activity:
public class MainActivity extends AppCompatActivity {
CountDownTimer myTimer;
Button btnStart;
Button btnCancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample2);
btnStart = (Button) findViewById(R.id.start);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
countme();
Toast.makeText(MainActivity.this, "Count Started!", Toast.LENGTH_SHORT).show();
}
});
btnCancel = (Button) findViewById(R.id.cancel_timer);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myTimer.cancel();
Toast.makeText(MainActivity.this, "Clicked Stop Timer!", Toast.LENGTH_SHORT).show();
}
});
}
private void countme() {
final int tick = 500;
final int countTime = 10000;
myTimer = new CountDownTimer(countTime, tick) {
#Override
public void onTick(final long millisUntilFinished) {
Log.d(MainActivity.class.getSimpleName(), "onTick()");
}
#Override
public void onFinish() {
// myPicture.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "In onFinish()", Toast.LENGTH_SHORT).show();
}
};
myTimer.start();
}
}
It works perfectly fine. It stops the timer. But I went and looked around and found this answer where it mentions that CountDownTimer doesn't seem to work, so he suggested to use a Timer instead. Do check it out. Cheers!
This is working example , I have implemented both handler and timer you can pick one .
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private CountDownTimer myTimer;
final int TICK = 500;
final int COUNT_DOWN_TIME = 2000;
// Option 2 using handler
private Handler myhandler = new Handler();
private Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Option 1 using timer
myTimer = new CountDownTimer(COUNT_DOWN_TIME, TICK) {
#Override
public void onTick(final long millisUntilFinished) {
((TextView) findViewById(R.id.textView3)).setText(""
+ millisUntilFinished);
}
#Override
public void onFinish() {
findViewById(R.id.timer_imageBiew).setVisibility(View.GONE);
}
};
// Option 2 using handler
runnable = new Runnable() {
#Override
public void run() {
findViewById(R.id.handlerImageView).setVisibility(View.GONE);
}
};
findViewById(R.id.start_timer).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
// Option 1 using timer
myTimer.start();
// Option 2 using handler
myhandler.postDelayed(runnable, COUNT_DOWN_TIME);
}
});
findViewById(R.id.stop_timer).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Option 1 stop timer
myTimer.cancel();
// Option 2 stop handler
myhandler.removeCallbacks(runnable);
}
});
}
}
I have a game where if a user touches the wrong button he goes to the highscores page and if he clicks the right one he goes to the next level. What I would like to do is make it so if the user does absolutely nothing for 1.5 seconds (fast-paced game) then it automatically intents him back to the scores.class activity. I am new to programming so anything helps!!! Thanks.
This will give you an idea:
private MainActivity context;
private CountDownTimer countDownTimer;
public boolean timerStopped;
/** Called when the activity is first created. */
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
startTimer();
// method looks at users choice, example
/* if (answer == true){
stopTimer();
// go to next question and start timer again..
}
else{
// do something
}
*/
}
/** Starts the timer **/
public void startTimer() {
setTimerStartListener();
timerStopped = false;
}
/** Stop the timer **/
public void stopTimer() {
countDownTimer.cancel();
timerStopped = true;
}
/** Timer method: CountDownTimer **/
private void setTimerStartListener() {
// will be called at every 1500 milliseconds i.e. every 1.5 second.
countDownTimer = new CountDownTimer(1500, 1500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// Here do what you like...
Intent intent = new Intent(context, Scores.class);
startActivity(intent);
}
}.start();
}
have you tried with CountDownTimer?
Here is an example:
new CountDownTimer(1500, 1500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// Here do what you like...
}
}.start();
To fix the error mentioned:
MainActivity context;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
new CountDownTimer(1500, 1500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// Here do what you like...
Intent intent = new Intent(context, Score.class);
startActivity(intent);
}
}.start();
I'm working with timer function in Android, I have the code of 1 to up timer. Please help me make it a countdown timer without clicking a button?
public class MainActivity extends Activity {
public int time = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView tv = (TextView) findViewById(R.id.main_timer_text);
tv.setText(String.valueOf(time));
time += 1;
}
});
}
}, 0, 1000);
}
You can use CountDownTimer for this
new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilEnd) {
mTextView.setText(String.valueOf(millisUntilEnd / 1000));
}
public void onFinish() {
mTextView.setText("done");
}
}.start();
This will update your time TextView each second for 20 seconds.
I'm creating an app that connect with server(localhost).
In my Activity, I have a countdown timer which will tick for 30 seconds and at the finish() I have some code to check if there's any changes in the database on server, and then start the timer again so it'll be a loop in the activity. But the timer seem like running just once. When I'm running in debug mode, the timer get some error at the finish(), it said that I'm missing some folder or file that related to the API or the SDK version which is being run in android:targetSdkVersion="9"
Please help me with this issue. Here's the activity:
public class Account_Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Creating the UI and some methods
counter = new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
//Codes for checking the changes in the database on the server
start();
}
};
counter.start();
}
}
Do I have to make a thread or not?
public class Account_Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Creating the UI and some methods
counter = new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
//Codes for checking the changes in the database on the server
new CountDownTimer(30000, 1000).start(); <------Start Again.
}
};
counter.start();
}
}
Can it possible to make a button on screen which automatically visible and gone every 5sec interval?
By using this
b.setVisibility(View.VISIBLE);
we can visible and
b.setVisibility(View.GONE);
we can hide it.But I can't manage to make it by usin the time interval.
Any idea?please share.
There are a few different ways, one is a Handler and Runnable:
public class Example extends Activity {
private Handler mHandler = new Handler();
private Runnable alternate = new Runnable() {
public void run() {
// Alternate visible and not
mHandler.postDelayed(alternate, 5000);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler.postDelayed(alternate, 5000);
}
}
Use this
new CountDownTimer(9000000, 5000) {
public void onTick(long millisUntilFinished) {
if(b.getVisibility() == View.GONE)
b.setVisibility(View.VISIBLE);
else
b.setVisibility(View.GONE);
}
public void onFinish() {
//Restart timer if you want.
}
}.start();