I want to make a timer which will be shown on every activity of my application.
I know how to make a timer on an activity below is my code
public class Timer extends Activity implements OnClickListener {
public TextView mTextField;
private Button btnstart;
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
seconds = seconds % 60;
minutes = minutes % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
output = minutesD + " : " + secondsD;
return output;
}
public CountDownTimer Counter1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Declare Start/Stop button
btnstart = (Button)findViewById(R.id.btnThread1);
btnstart.setOnClickListener(this);
//Button btnstop = (Button)findViewById(R.id.button02);
//Button btnpass = (Button)findViewById(R.id.button03);
//Declare Text fields to show time left
final TextView mCounter1TextField=(TextView)findViewById(R.id.txtThread1);
final TextView mCounter2TextField = (TextView)findViewById(R.id.txtThread2);
//final TextView mCounter3TextField=(TextView)findViewById(R.id.textView03);
//Counter 1
Counter1 = new CountDownTimer(120000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
}
public void onFinish() {
mCounter1TextField.setText("Finished!");
// Counter1.start();
}
};
}
@Override
public void onClick(View v) {
if(v == btnstart)
{
Counter1.start();
}
}
}
I want to know how to make it global??
You can have a singleton class holding your
CountDownTimer
Second option is to have your timer as a member of a custom Application class: Use Application class for global variables
Third option : make a local service that starts the timer when started.
Keep in mind that in case of options 1 and 2, if the OS decides to kill you app, the timer will vanish (i.e. when the activity is re-created, the CountDownTimer object will be resetted).
Create a Singleton! http://en.wikipedia.org/wiki/Singleton_pattern
singleton guarantee that there will be only one object of some kind and makes it easy for any other object to access it.
make a singleton class and register listener in your ui.
in CountDownTimer class
If you want to show it one every activity of your application, then I suggest you to use Fragments. All you need to do is to create a Fragment with your Timer class that will appear every time, another Fragment with other content you want to show, and manipulate the lifecycle of the second Fragment only.
I have solved my problem by making a timer in one activity and calling its protected function in my other activity.
This is the startTimer function to start the timer:
and this is the public function which can be used by any other activity:
and this is how I am getting the value on other activity: