when restart CountDownTimer, it finish work - java

I want to stop CountDownTimer and to restart it, with time when it stopped. I have this code in class CountDownTimer
public class MyDownTimer extends CountDownTimer{
long mills;
public MyDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.mills=millisInFuture;
}
#Override
public void onFinish() {
GameScreen.showWindow("Time is over","Sorry, time is Over, you lose");
}
#Override
public void onTick(long millisUntilFinished) {
mills=millisUntilFinished/1000;
GameManager.time= millisUntilFinished/1000;
}
}
and Dialog class, he must to showing, when user click in button 'pause'.
Dialog code, as you can see, i start new CountDownTimer in this code
public static void showWindowPause(final MyDownTimer dTime){
final Dialog wdialog= new Dialog(context);
wdialog.setContentView(R.layout.dialog_window);
wdialog.setTitle("title");
TextView text=(TextView)wdialog.findViewById(R.id.txtFirstWord);
text.setText("mainText");
Button dialogButton=(Button)wdialog.findViewById(R.id.button1);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
wdialog.dismiss();
dTime.start();
}
});
wdialog.show();
}
and method, where I stoping CountDownTimer and show Dialog window.
long stime =dTime.mills;
dTime.cancel();
dTime=null;
dTime= new MyDownTimer(stime,1000);
GameScreen.showWindowPause(dTime);
when i click button in Dialog window, i see new Dialog, which created in method
MyDownTimer.onFinish()
But if I change line
long stime =dTime.mills;
on
long stime =5000;
CountDownTimer restarts,and all normal.
where i made mistake?

Your question is really confusing , but I THINK I found something that doesn't make sense.
public void onTick(long millisUntilFinished) {
mills=millisUntilFinished/1000;
GameManager.time= millisUntilFinished/1000;
}
I think it should be
mills=millisUntilFinished - 1000;
edit: If you are actually converting millseconds to seconds, then you'll need to do
long stime =dTime.mills*1000;

Related

Android keep CountDownTimer running when app is in the background

In my app, I implement CountDownTimer in my listview as following:
OrdersCountDownTimer.java
public class OrdersCountDownTimer extends CountDownTimer {
private OrdersAdapter adapter;
public OrdersCountDownTimer(long millisInFuture, long countDownInterval, OrdersAdapter adapter) {
super(millisInFuture, countDownInterval);
this.adapter = adapter;
}
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
adapter.decTime();
adapter.notifyDataSetChanged();
this.start();
}
}
OrdersActivity.java
timer = new OrdersCountDownTimer(ONE_SECOND, ONE_SECOND, adapter);
So, I'm decrementing the value in my Order object upon every tick my timer finishes.
The problem is that this is running in my main thread, which means when my app goes to the background or the screen is locked, the timer is paused as well.
I would like to keep the timer running at all times. Is there a way to do this?

How can I tell MyCountDownTimer to quit when the activity is not the current activity?

The project i'm working on is an quiz with a timer on each question. for 30 seconds. I noticed that if you finish the test before the timer runs out, the timer doesn't stop running. So if you head on to another test, the notification that you haven't finished the test will popup and override the current activity. I tried using the cancel() method, but i'm sure I misplaced it.
Here is a snippet of the MyCountDownTimer Class
public MyCountDownTimer(TextView textCounter, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.textCounter = textCounter;
}
#Override
public void onTick(long millisUntilFinished) {
textCounter.setText(String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
Intent retryIntent = new Intent(textCounter.getContext(), Retry.class);
if (textCounter.getContext() instanceof Test1){
whichTest = 1;
retryIntent.putExtra("whichTest",whichTest);
}
textCounter.getContext().startActivity(retryIntent);
}
This is a snippet of the Activity that implements the method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_page);
textCounter = ((TextView)findViewById(R.id.textCounter));
myCountDownTimer = new MyCountDownTimer(textCounter, 29000, 1000);
myCountDownTimer.start();
textCounter.setText("");
myCountDownTimer.onTick(29000);
}#Override
public void onClick(View v) {
if (questionIndex == questions7.length){
myCountDownTimer.cancel();
Intent intent1 = new Intent(Test1.this, UsersAnswers1.class);
intent1.putExtra("usersAnswers1", usersAnswers1);
intent1.putExtra("isATOF1", isATOF1);
intent1.putExtra("score1S", score1S);
startActivity(intent1);
}
}
Override onStop method of your activity and use code like
#Override
protected void onStop() {
myCountDownTimer.cancel();
super.onStop();
}
Hence whenever your activity goes in background it will cancel any timer associated with current activity.

Pass the name of button clicked to different class in Android

I am new to android (and not so hot with it, but trying to learn).
I am creating an application that has a number of various buttons that start countdown timers when they are clicked.
On the activity that has these buttons the following code is used to start the timer:
//Button 1 Start On Click
final CountDown buttonOneTimer = new CountDown(15000,1000);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
buttonOneTimer.start();
}
});
//Button 2 Start On Click
final CountDown buttonTwoTimer = new CountDown(15000,1000);
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
buttonTwoTimer.start();
}
});
my CountDown class looks like this:
public class CountDown extends CountDownTimer {
public CountDown (long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
System.out.println("Timer Completed.");
****NAMEOFTHEBUTTONCLICKED****.setText("Timer Completed.");
}
#Override
public void onTick(long millisUntilFinished) {
****NAMEOFTHEBUTTONCLICKED****.setText((millisUntilFinished/1000)+"");
System.out.println("Timer : " + (millisUntilFinished/1000));
}
}
I am trying to get the name of the button pressed into the class so I can set the text on the button to countdown.
I'm sure I am doing all sorts of things wrong or there may be better ways to do it - so if you see areas where I could improve - feel free to critique me!
I read over the tutorial here however it has an 'inner class' (I believe that is what it is called?) inside the current class. A friend of mine said that's very rarely done and to just create a separate class such as CountDown. If I do it the same way as in the tutorial I can get the timer to work (by hardcoding the buttons name where it says *NAMEOFTHEBUTTONCLICKED* above, which means it only works for that button) - but I still need to figure out how to pass that class the buttons name so I don't have to write a separate class for each timer.
Would that be done through Extras? I have had a hard time finding any more info to my specific issue. Any help is appreciated!!
Try passing off the button instance to the Timer:
public class CountDown extends CountDownTimer {
Button button;
public CountDown (long millisInFuture, long countDownInterval, Button button) {
super(millisInFuture, countDownInterval);
this.button = button;
}
#Override
public void onFinish() {
System.out.println("Timer Completed.");
button.setText("Timer Completed.");
}
#Override
public void onTick(long millisUntilFinished) {
button.setText((millisUntilFinished/1000)+"");
System.out.println("Timer : " + (millisUntilFinished/1000));
}
}
Then call like this:
//Button 1 Start On Click
final CountDown buttonOneTimer = new CountDown(15000,1000,buttonOne);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
buttonOneTimer.start();
}
});

How do you make a StopWatch in android using the chronometer?

I'm trying to make a stopwatch for android as part of a score board app the problem is I'm not sure how to use the chronometer to start, stop, then resume from the time it stopped at or reset to 0. The only problem is that I'm not sure how to use the setBase() and getBase() methods properly.
public class MainActivity extends Activity {
int homecount, awaycount, minutes, seconds;
TextView home, away;
Button stop, start, reset, addhome, subhome, addaway, subaway;
TextView time;
public boolean running;
Chronometer MyChronometer;
long startTime, stopTime;
running = false;
stopTime = 0;
MyChronometer = (Chronometer) findViewById(R.id.timer);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((Chronometer) findViewById(R.id.timer)).start();
startTime=System.currentTimeMillis();
running = true;
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((Chronometer) findViewById(R.id.timer)).stop();
stopTime=System.currentTimeMillis();
running = false;
}
});
reset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
You have the variable MyChronometer. Use that every time you want to start or stop.
Also, right after initialization you should run MyChronometer.setBase(SystemClock.elapsedRealtime());. For more details read the documentation: Chronometer and SystemClock.

checking for variable changes within a specified amount of time?

I'm trying to check for variable changes within a specified amount of time. For example I got a variable:
int downloadProgress = 0;
and I got some code which downloads a file and the var downloadProgress is being update to 1..2.3....5.6..7. until ..100 (download finishes).
Now I want to have a thread like the following:
Runnable checkNet = new Runnable(){
public void run(){
// check variable changes
}
};
that checks for changes in the variable downloadProgress within a specified amount of time say 20 seconds. If the variable doesn't change for at least 20 seconds than I want to do something!
How could I utilize a timer to do this within a thread!!
you may use timer: http://developer.android.com/reference/java/util/Timer.html In this case you will not need a thread, because Timer imlies a thread
Here is example:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
MyCount counter = new MyCount(5000,1000);
counter.start();
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
tv.setText(”done!”);
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText(”Left: ” + millisUntilFinished/1000);
}
}
}
Source: http://dewful.com/?p=3

Categories

Resources