There are three buttons in class A and class B.CheckIn,Cancel,Complete. Click on one of these buttons leads to corresponding activity in the class B.I am saving the current time in the chronometer in a database with the help of Helper class once in a minute.But I want to continue run the time even I go to the other activities. But I am getting the latest updated time while I was in the Activity.So is there is any solution to run the chronometer even I switch between the activities?? Thanks In advance!!!
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.checkin_timer);
taskId=getIntent().getIntExtra("taskId", 1);
status=getIntent().getIntExtra("status", 0);
String values1[]={Integer.toString(taskId)};
//Helper class retrieves the time spent with given parametrs
System.out.println(Helper.getfromUrl(getTimeSpentUrl,values1));
timeSpent=Long.parseLong(Helper.getfromUrl(getTimeSpentUrl,values1));
chrono=(Chronometer)findViewById(R.id.chrono);
btnCancel=(Button)findViewById(R.id.btnCancel);
btnCheckIn=(Button)findViewById(R.id.btnCheckIn);
btnComplete=(Button)findViewById(R.id.btnComplete);
chrono.setText(formatTime(timeSpent));
System.out.println(timeSpent);
chrono.setOnChronometerTickListener(new OnChronometerTickListener()
{
public void onChronometerTick(Chronometer arg0) {
// TODO Auto-generated method stub
time++;
if(!resume)
{
currentTime=formatTime(timeSpent+SystemClock.elapsedRealtime()-chrono.getBase());
arg0.setText(currentTime);
elapsedTime=SystemClock.elapsedRealtime()+timeSpent;
}
else
{
currentTime=formatTime(elapsedTime-chrono.getBase());
arg0.setText(currentTime);
elapsedTime=elapsedTime+1000;
}
if( time== 60 )
{
String values2[]={Integer.toString(taskId), Long.toString(elapsedTime-chrono.getBase())};
String updateTime=Helper.getfromUrl(updateTimeUrl,values2);
if (!updateTime.equals("success"))
{
Toast.makeText(getApplicationContext(), "Not updated", Toast.LENGTH_SHORT).show();
}
time=0;
}
}
});
if(status==0)
{
flag=1;
this.onClick(btnComplete);
}
else if(status==1)
{
this.onClick(btnCheckIn);
}
else if(status==2)
{
flag=1;
this.onClick(btnCancel);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnCheckIn:
btnCheckIn.setEnabled(false);
btnCancel.setEnabled(true);
if(!resume)
{
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
}
else
{
chrono.start();
}
String values[]={Integer.toString(taskId),Integer.toString(1)};
String updateCheckIn=Helper.getfromUrl(updateCheckinUrl, values);
if (!updateCheckIn.equals("success"))
{
Toast.makeText(getApplicationContext(), "Not updated", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnCancel:
btnCheckIn.setEnabled(true);
btnCancel.setEnabled(false);
chrono.stop();
String values1[]={Integer.toString(taskId),Integer.toString(0)};
String updateCheckIn1=Helper.getfromUrl(updateCheckinUrl, values1);
if (!updateCheckIn1.equals("success"))
{
Toast.makeText(getApplicationContext(), "Not updated", Toast.LENGTH_SHORT).show();
}
if(flag==1)
{
chrono.setText(formatTime(timeSpent));
}
else
{
resume=true;
chrono.setText(formatTime(elapsedTime-chrono.getBase()));
String values2[]={Integer.toString(taskId), Long.toString(elapsedTime-chrono.getBase())};
String updateTime=Helper.getfromUrl(updateTimeUrl,values2);
if (!updateTime.equals("success"))
{
Toast.makeText(getApplicationContext(), "Not updated", Toast.LENGTH_SHORT).show();
}
}
flag=0;
Intent reasonIn = new Intent(getApplicationContext(), Reason.class);
startActivity(reasonIn);
break;
case R.id.btnComplete:
btnCheckIn.setEnabled(false);
btnCancel.setEnabled(false);
chrono.stop();
resume=true;
String values11[]={Integer.toString(taskId),Integer.toString(0)};
String updateCheckIn11=Helper.getfromUrl(updateCheckinUrl, values11);
if (!updateCheckIn11.equals("success"))
{
Toast.makeText(getApplicationContext(), "Not updated", Toast.LENGTH_SHORT).show();
}
String values3[]={Integer.toString(taskId),Integer.toString(1)};
String updateComplete=Helper.getfromUrl(updateCompleteUrl, values3);
if (!updateComplete.equals("success"))
{
Toast.makeText(getApplicationContext(), "Not updated", Toast.LENGTH_SHORT).show();
}
if(flag==1)
{
chrono.setText(formatTime(timeSpent));
}
else
{
chrono.setText(formatTime(elapsedTime-chrono.getBase()));
String values21[]={Integer.toString(taskId), Long.toString(elapsedTime-chrono.getBase())};
String updateTime1=Helper.getfromUrl(updateTimeUrl,values21);
if (!updateTime1.equals("success"))
{
Toast.makeText(getApplicationContext(), "Not updated", Toast.LENGTH_SHORT).show();
}
}
Intent completeIn = new Intent(getApplicationContext(), CompletionStatus.class);
startActivity(completeIn);
finish();
break;
}
}
public String formatTime(long millis) {
String output = "00:00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
if (hours < 10)
hoursD = "0" + hours;
output = hoursD + " : " + minutesD + " : " + secondsD;
return output;
}
}