0
if (totalKm<Contsants.minDist)
{
  float totalfare=Contsants.minFare;
  mb.putString("Fare", String.format("%.2f",(totalfare)));
  //Contsants.jobTotalFare=totalfare;

}
else
    {
   float totalfare= Contsants.minFare+ ((totalKm-Contsants.minDist)  *Contsants.rupeeKm)  +(Contsants.cont_WaitingTimeInSec/60)*1;
   mb.putString("Fare", String.format("%.2f",(totalfare)));
   //Contsants.jobTotalFare=totalfare;

}

我尝试使用上述条件更新当前屏幕中的总票价。但问题是 if 条件有效并在屏幕上显示了当前的总票价。总是它只显示一个 if 部分计算。我不知道为什么屏幕上没有更新 else 条件。我如何更新其他条件当前的总票价。谁来帮帮我。

完整代码:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.jobstartedscreen);
if (savedInstanceState==null){
    Contsants.VehicleStartOdometer=0.0F;
    sThread=new MyThread(mHandler);
    sThread.start();
}
tcpSocket.setmMessageListener(this);
tv_JobNo =(TextView)findViewById(R.id.tvJobid);
Bundle bundle = getIntent().getExtras();
jobno= bundle.get("Jobno").toString();
tv_JobNo.setText(jobno);

tv_JobKm =(TextView)findViewById(R.id.tvjobKm);
tv_JobwaitngTime =(TextView)findViewById(R.id.tvjobWaittime);
tv_speed=(TextView)findViewById(R.id.speed);
tv_Fare=(TextView)findViewById(R.id.tv_fare);

tv_JobwaitngTime.setText("Waiting Time : 0:0");
//update the Km every 1 sec
Log.d(tag, "On created "+_ResumeThread);
tvjobstartedtime=(TextView)findViewById(R.id.tv_jobStarttime);
tvjobstartedtime.setText("Start Time :"+Contsants.jobStartTime);

btn_jobFinsh= (Button)findViewById(R.id.btn_jobFinsh);
btn_jobFinsh.setOnClickListener(onClickListener);

btn_msg= (Button)findViewById(R.id.btn_msg);
btn_msg.setOnClickListener(onClickListener);

StateManager.DeviceStatus=StateManager.hired;


   // TODO Auto-generated method stub
   }
 @Override
  public void onStart()
{
   super.onStart();

  }
  @Override
 public void onResume()
{
super.onResume();
w = this.getWindow();
w.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
w.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
w.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
Log.d("job start on resume",_ThreadFlag+" "+_ResumeThread);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
if (sThread.isAlive()){
    sThread.setHandler(mHandler);
}
}

@Override
protected void onDestroy(){
super.onDestroy();
if (sThread.isAlive()){
    sThread.setHandler(null);
}
sThread.setHandler(null);
}

 boolean _Paused=false;
@Override
 public void onPause()
{
super.onPause();
_Paused=true;
}
 @Override
 public void onBackPressed() {
// do nothing.
}

private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
    switch(v.getId()){
    case R.id.btn_jobFinsh:
        //DO something
        _ThreadFlag=true;
        StateManager.JobStatus=StateManager.jobstatus_dropped;
        tcpSocket.SendMessage(FramePacket.FrameJobStatusCmd(Contsants.Jobno,StateManager.JobStatus));
         SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy HH:mm");
         String currentDateandTime = sdf.format(new Date());
         Contsants.jobEndTime=currentDateandTime;
         Contsants.jobEndKm =gpsdataElements.Distance;
        Intent i = new Intent(getApplicationContext(),JobCompletionScreen.class);
        startActivity(i);
        sThread.setHandler(null);
        finish();
        break;
    case R.id.btn_msg:
        /*Intent iNAV = new Intent(Intent.ACTION_VIEW,
                Uri.parse("http://maps.google.com/maps?saddr=12.920629,77.610643&daddr=12.990346, 77.589521")
                );
        startActivity(iNAV);*/
        Intent imsg = new Intent(getApplicationContext(),MessageList.class);
        startActivity(imsg);
        break;


    }
}
};

 static Handler mHandler =new Handler(){
@Override
public void handleMessage(Message message){
    //update UI
    Bundle b = message.getData();
    float odometer = b.getFloat("My Odometer");
    String Fare = b.getString("Fare");
    int waiting =b.getInt("waiting Time");
    double speedVal= b.getDouble("SpeedValue");
    tv_speed.setText(String.format("%.2f",speedVal)+" Km/hr");
    tv_JobKm.setText(String.format("%.2f",odometer)+" Km");
    tv_Fare.setText(Fare);  
    tv_JobwaitngTime.setText(Integer.toString((waiting / 60)) +":"+ Integer.toString((waiting% 60)));
    Contsants.jobTotalwaitingTime=Integer.toString((waiting / 60)) +":"+ Integer.toString((waiting% 60));
}
};

private class MyThread extends Thread{
private Handler mHandler;

public MyThread(Handler handler){
    super();
    mHandler=handler;
}

@Override
public void run(){
    //some long operation
    if (mHandler!=null)
        mHandler.sendEmptyMessage(0);

    _ResumeThread=true;
    while(!_ThreadFlag)
    {
        try {
            Thread.sleep(1000);   
            Message msg = new Message();
            Bundle b = new Bundle();
            b.putFloat("My Odometer", (float) (gpsdataElements.Distance-Contsants.jobStartKm));
            if(gpsdataElements.Speed<1)
            {
                Contsants.cont_WaitingTimeInSec++;
            }
            float totalKm =  Contsants.jobEndKm-Contsants.jobStartKm ;

            if (totalKm<Contsants.minDist)
            {
                //jcWaitingFare.setText("0");
                float totalfare=Contsants.minFare;
                b.putString("Fare", String.format("%.2f",(totalfare)));
            }
            else
            {
                float totalfare= Contsants.minFare+ ((totalKm-Contsants.minDist) *Contsants.rupeeKm)  +(Contsants.cont_WaitingTimeInSec/60)*1;
                b.putString("Fare", String.format("%.2f",(totalfare)));
            }

            b.putInt("waiting Time",  Contsants.cont_WaitingTimeInSec);
            b.putDouble("SpeedValue", gpsdataElements.Speed*1.852);
            //  tv_JobwaitngTime.setText("Waiting Time : "+Integer.toString((Contsants.cont_WaitingTimeInSec / 60)) +":"+ Integer.toString((Contsants.cont_WaitingTimeInSec % 60)));
            //send message to the handler with the current message handler          
            msg.setData(b);
            // send message to the handler with the current message handler
            mHandler.sendMessage(msg);

        } catch (Exception e) {
            Log.v("Error", e.toString());
        }
    }
}

public void setHandler(Handler handler){
    mHandler=handler;
}
}

 @Override
public void messageReceived(String message) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Job StartScreem : Recived data :"+message,    Toast.LENGTH_LONG).show();        
}

}
4

0 回答 0