A类和B类有三个按钮.CheckIn,Cancel,Complete。单击这些按钮之一导致BI类中的相应活动在一分钟内在Helper类的帮助下将计时器中的当前时间保存在数据库中。但是我想继续运行时间,即使我去了另一个活动。但是当我在Activity.So时,我得到了最新的更新时间。有没有任何解决方案来运行计时器甚至我在活动之间切换?在此先感谢!!!
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;
}
}
答案 0 :(得分:2)
setBase()
用于设置倒计时器参考的时间。
。您需要在活动之外的某个位置保持此值,例如Application
类。每当您的活动恢复时,您setBase()
到此参考时间。