安卓:为了保持活动的时间天文台天文台、时间

由网友(小喜)分享简介:有A类和类B.CheckIn三个按钮,取消,完整。点击这些按钮之一导致BI现在的储蓄在天文台当前时间与Helper类的帮助下,数据库类对应的活动曾经在minute.But我想继续运行的时间,即使我去其他活动。但我得到了最新更新的时间,而我是在Activity.So是有什么解决方案来运行我的活动之间切换时计什??在此先感...

有A类和类B.CheckIn三个按钮,取消,完整。点击这些按钮之一导致BI现在的储蓄在天文台当前时间与Helper类的帮助下,数据库类对应的活动曾经在minute.But我想继续运行的时间,即使我去其他活动。但我得到了最新更新的时间,而我是在Activity.So是有什么解决方案来运行我的活动之间切换时计什??在此先感谢!

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;
   }

}

推荐答案

setBase()用来设置该向上计数器是在参考时间。。你需要的地方举行这个值的活动外,像应用类。每当你的活动有简历,你 setBase()这个参考时间。

The setBase() is used to set the time that the count-up timer is in reference to. . You need to hold on to this value somewhere outside the activity, like in the Application class. Whenever your activity has resumes, you setBase() to this reference time.

阅读全文

相关推荐

最新文章