Adarsh
Adarsh

Reputation: 21

Creating a Singleton CountDownTimer in Android

I'm a beginner in android and I've written an activity. It contains a CountDownTimer that counts down from a particular value. It also contains a Button that loads text information and a textview to display count.

Below is the code for Activity1:

public class Screen extends Activity1 implements OnClickListener {  
private static final int MILLIS_PER_SECOND = 1000;  
private static final int SECONDS_TO_COUNTDOWN = 1;  
TextView Time;  
int totaltime;  
Button startTimer, howTo, pause;  
protected CountDownTimer MyTimer;  
int PracticeCount;  
long tot;  

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pushupscreen);
    getRefs();
    getSpeed();
            getCount();
    setTotalTime();
    startTimer.setOnClickListener(this);
    pause.setOnClickListener(this);
}

private void getRefs() {
    // Initialize layout resources
    Time = (TextView) findViewById(R.id.tvTime);

    startTimer = (Button) findViewById(R.id.bStart);
    howTo = (Button) findViewById(R.id.btHowTo);
    pause = (Button) findViewById(R.id.bPause);
    howTo.setOnClickListener(this);

}

  private void getTheCount() { 
 //get count from SharedPreferences
      }



  private void getSpeed() { 
   //get speed from SharedPreferences
          } 

 private void setCount(){
        totalTime=speed*count;}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == startTimer) {

        try {
            showTimer(time);

        } catch (NumberFormatException e) {
            // method ignores invalid (non-integer) input and waits
            // for something it cant use
        }
    } else if (v == pause) {
        MyTimer.cancel();
        Timer.setText("Resume");
    } else if (v == howTo) {

        //Launch screen containing information
    }
}

private void showTimer(long time) {
    if (MyTimer != null) {
        MyTimer.cancel();
    }

    MyTimer = new CountDownTimer(tot2, MILLIS_PER_SECOND) {
        @Override
        public void onTick(long millisUntilFinished) {
            tot = millisUntilFinished;
            long seconds = millisUntilFinished / 1000;
            Time.setText(String.format("%02d", seconds / 60) + ":"
                    + String.format("%02d", seconds % 60));

        }

        @Override
        public void onFinish() {
            Time.setText("KABOOM!");

        }
    }.start();
}

}

And here is the layout file for this:

<TextView
    android:id="@+id/tvTime"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:gravity="center"
    android:padding="10dip"
    android:text="@string/starttime"
    android:textSize="60sp" />

<Button
    android:id="@+id/bStart"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tvTime"
    android:text="Start" />

<Button
    android:id="@+id/bPause"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tvTime"
    android:layout_toRightOf="@+id/btHowTo"
    android:text="Pause" />

<TextView
    android:id="@+id/tvCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btHowTo"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="39dp"
    android:text="25" 
    android:textSize="80sp"
    android:textAlignment="center"/>

My questions:

1.How do I create 4 activities that use the same layout and the same timer? Each Activity loads a different content in the textview and a different screen on the click of HowTo button.
2.How can Activity1 be designed to run for 1/4th the time set and pass remaining time to Activity2? Is it possible?

I would really appreciate any help and advice that you can provide. Thanks.

Upvotes: 2

Views: 2750

Answers (2)

Matko Smoljan
Matko Smoljan

Reputation: 1

  1. You could make a custom control, which is basically a new class which inherits some other control's class (for example a LinearLayout or a RelativeLayout). You could then load a view's XML to your new layout or programmatically create new controls inside your control. More info here: Custom components in Android

  2. After a 1/4 of your countdown period, you can create and send an Intent to start a new activity in the onTick method. You can also put the remaining 3/4 as a millisecond value (of type long) in an intent extra. You can then obtain this value in the new activity and invoke a custom CountDownTimer child there for the rest of your countdown. Then you can finally execute what you wish after the countdown is done in the onFinish() method.

Upvotes: 0

akhalsa
akhalsa

Reputation: 2313

A couple things here.

  1. Its very easy to re-use layouts. In each activity's onCreate you would just call: setContentView(R.layout.pushupscreen); The pushupscreen.xml file can be shared across all activities this way.

  2. What you probably want to do is persist a timestamp to some common data source for all the activities. This could be a write to a SharedPreferences file: Documentation here. Then as each activity resumes, check how much time has already passed by comparing this timestamp to the current timestamp. You could also pass the timestamp as an extra in the intent to start up the subsequent activities. The documentation for that can be found here and here

Upvotes: 1

Related Questions