如何获取当前活动输出并将其设置为显示?

时间:2013-09-09 11:20:47

标签: android distance

我已经完成了一个用于出租车预订的Android应用程序。在这个我设计的两个屏幕。一个被命名为工作开始屏幕,另一个被命名为工作完成屏幕。在作业开始屏幕中,我使用if else语句编写了两个条件。如果条件我将最低票价设为4公里为100。如果toatalkm超过minDist意味着我去了其他条件,在这里我为每公里做一些计算,加上10卢比的最低票价和等待时间。如下所示。

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

我想将当前票价详细信息显示在作业已完成屏幕中。我不知道如何从上述声明中获取当前票价详细信息并将其设置为作业完成活动。 今天只测试了我的应用程序然后我才知道该应用程序在作业启动屏幕上挂起。

下面是我的屏幕。这里的计算没有正确显示。单击完成按钮时,它将移至作业完成活动。在这项活动中,我们设计了总票价和等待时间。如何从此屏幕获取当前详细信息并将其设置为作业已完成屏幕。

enter image description here 工作完成的屏幕类:

private TextView jcJobNo;
private TextView jcStartTime;
private TextView jcEndTime;
public TextView jcTotalKms;
public TextView jcTotalFare;
public TextView jcWaitingFare;

private Button btn_JobCompleted;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_jobcompletedscreen);
    tcpSocket.setmMessageListener(this);

    StateManager.DeviceStatus=StateManager.free;

    StateManager.JobStatus=StateManager.jobstatus_completed;
    tcpSocket.SendMessage(FramePacket.FrameJobStatusCmd(Contsants.Jobno, StateManager.JobStatus));

    jcJobNo=(TextView)findViewById(R.id.tv_jCjobno);
    jcStartTime= (TextView)findViewById(R.id.tv_JCStartTime);
    jcEndTime= (TextView)findViewById(R.id.tv_JCEndTime);
    jcTotalKms= (TextView)findViewById(R.id.tv_JCTotalKMs);
    jcTotalFare= (TextView)findViewById(R.id.tv_JCTotalFare);

    btn_JobCompleted = (Button)findViewById(R.id.btn_jcFinsh);
    btn_JobCompleted.setOnClickListener(onclicklistner);

    jcWaitingFare=(TextView)findViewById(R.id.tv_JCWaitingFare);

    jcJobNo.setText(Contsants.Jobno);
    jcStartTime.setText(Contsants.jobStartTime);
    jcEndTime.setText(Contsants.jobEndTime);
    // total distance finding here
    float totalKm =  Contsants.jobEndKm-Contsants.jobStartKm ;
    jcTotalKms.setText(String.format("%.2f",totalKm));
    //jcTotalKms.setText(Float.toString((float) (totalKm/16.0)));
    //finding total fare here
    //int value=100;
    if (totalKm<Contsants.minDist)
    {
        jcWaitingFare.setText("0");
        float totalfare=Contsants.minFare;
        jcTotalFare.setText(String.format("%.2f",(totalfare)));
        Contsants.jobTotalKm= totalKm;
        Contsants.jobTotalFare=totalfare;
    }
    else
    {
        jcWaitingFare.setText(Integer.toString((Contsants.cont_WaitingTimeInSec/60)*1));
        float totalfare= Contsants.minFare+ ((totalKm-Contsants.minDist) *Contsants.rupeeKm)  +(Contsants.cont_WaitingTimeInSec/60)*1;
        jcTotalFare.setText(String.format("%.2f",(totalfare)));
        Contsants.jobTotalKm= totalKm;
        Contsants.jobTotalFare=totalfare;
    }
}
private OnClickListener onclicklistner = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
        case R.id.btn_jcFinsh:

            tcpSocket.SendMessage(FramePacket.FrameJobFarecmd(Contsants.jobTotalKm, Contsants.jobTotalFare, Contsants.Jobno, Contsants.jobTotalwaitingTime,Contsants.jobOncallDistacnce));

            Contsants.cont_WaitingTimeInSec=0;
            Contsants.Jobno="";
            Contsants.JobCustomerName="";
            Contsants.JobCustomerNo="";
            Contsants.JobFromAdd="";
            Contsants.JobToAdd="";
            Contsants.JobAdditionalInfo="";
            Contsants.JobpickupTime="";
            Contsants.JobString="";
            Contsants.VehicleEndOdometer=0.0F;
            Contsants.VehicleStartOdometer=0.0F;
            Contsants.jobDistance=0.0F;
            Contsants.jobEndKm=0.0F;
            Contsants.jobStartKm=0.0F;
            Contsants.jobOncallDistacnce=0.0F;

            Contsants.jobEndTime="";
            Contsants.jobStartTime="";

            Contsants.JobString="";

            Contsants.jobTotalwaitingTime = "";
            Contsants.jobTotalFare= 0.0F;
            Contsants.jobTotalKm= 0.0F;

            Intent i = new Intent(getApplicationContext(),FreeScreen.class);
            startActivity(i);       
            break;
        }
    }
};

@Override
public void onBackPressed() {
    // do nothing.
    super.onBackPressed();
    finish();
}


@Override
public void messageReceived(String message) {
    // TODO Auto-generated method stub

}

 }

作业开始画面类:

TextView tv_JobNo;
static TextView tv_JobKm;
static TextView tv_speed;
static TextView tv_JobwaitngTime;
TextView tvjobstartedtime;
Button btn_jobFinsh;
Button btn_msg;
ImageButton navig;
boolean _ThreadFlag=false;
boolean _ResumeThread=false;
private Window w;
Thread t=null;
String jobno;
String tag="Jobstartscreen";
private static MyThread sThread;
static TextView tv_Fare;
private TextView jcTotalFare;
private TextView jcWaitingFare;

/** Called when the activity is first created. */
@Override
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();        
}

 }

2 个答案:

答案 0 :(得分:2)

这可以使用Intent实现,

在您的第一个活动中:

制作 totalFare global(将其从if block中删除),以便将其传递给Intent

现在,只需将额外内容添加到您点击完成按钮时调用的Intent

Intent intent = new Intent(this, JobCompleted.class);
intent.putExtra("totalFare", totalFare+"");
startActivity(intent);

在第二项活动中(onCreate()):

String totalFare = getIntent().getExtras().getString("totalFare");

现在在TextView上显示此文字。

答案 1 :(得分:0)

在完成按钮单击

中使用此代码
   protected SharedPreferences oSharedPreferences;
   protected Editor edit;
   oSharedPreferences = HomeActivity.this.getSharedPreferences(
            "userdetails", MODE_PRIVATE);

  edit = oSharedPreferences.edit();
  btn_finish.setOnClickListener(new OnClickListener()
    {

        @Override
    public void onClick(View v)
        {
            // TODO Auto-generated method stub
                 edit.putString("totalfare", "255");// key-totalfare value-255
                 edit.commit();
                 Intent passFare = new Intent(this, JobCompleted.class);

                 startActivity(passFare);

        }
    });

从sharepreference获取值使用下面在jobcompleted类

中给出的代码
   protected SharedPreferences oSharedPreferences;
   protected Editor edit;
   oSharedPreferences = HomeActivity.this.getSharedPreferences(
            "userdetails", MODE_PRIVATE);

   edit = oSharedPreferences.edit();

   String value=oSharedPreferences.getString("totalfare", "")
   System.out.println("*******value*******"+value);