0

I have completed one android application for taxi booking. In this i designed two screens. one is named as job started screen and anther one is named as job completion screen. In job started screen i have wrote two conditions using if else statement. In if condition i set minimum fare as 100 for 4 kilometers. if toatalkm goes above minDist means i go for else condition and here i make some calculations for every km add 10 rupee with that minimum fare and for waiting time. like below i shown.

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

I want to display the current fare details into the job completed screen. I dont know how to get the current fare details from the above statement and set it to the job completion activity. today only tested my app. then only i came to know that application hangs up on the job started screen.

below is my screen. here the calculation is not displayed properly. when click on the finish button it moves to job completed activity. in this activity we designed to see the total fare and waiting time. how to get the current details from this screen and set it to the job completed screen.

enter image description here job completed screen class:

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

}

 }

job started screen class:

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

 }
Ramesh
  • 35
  • 3
  • 9
  • Make totalfare as public variable and make instance of that class in which you get totalfare and by calling instance of it and then variable you get its value – ADT Sep 09 '13 at 11:27
  • [User Intent Extras to pass data to next activity](http://stackoverflow.com/q/4233873/1008278) or use Getters and Setters. – VenomVendor Sep 09 '13 at 11:45

2 Answers2

2

This can be achieved using an Intent,

In your first Activity:

make the totalFare global (by taking it out of if block) so you can pass it to an Intent.

Now, just add the extra to your Intent which is called on clicking the Finish Button.

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

And in the second activity (in onCreate()):

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

Show this text on a TextView now.

Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
  • can i update my job started and job completed class now..u see that and tell me where to change. @twntee – Ramesh Sep 09 '13 at 11:28
  • now i updated man. just show me in my code where i want to change. – Ramesh Sep 09 '13 at 11:34
  • @user2738655 try the above code, add the Intent Extra to `onClick()` of your Finish button in JobStarted, and retrieve the extra in `onCreate()` of JobCompleted – Rachit Mishra Sep 09 '13 at 11:34
  • wait am going to check that. – Ramesh Sep 09 '13 at 11:37
  • The constructor Intent(new View.OnClickListener(){}, Class) is undefined. it shows this error when added into onclick. what can i do. – Ramesh Sep 09 '13 at 11:40
  • can u make it more clearly means its very useful for me bro. so kindly show it in my code. – Ramesh Sep 09 '13 at 11:41
  • @user2738655 the code you have posted is from the JobCompleted Activity itself, so why do you want to pass it again ! – Rachit Mishra Sep 09 '13 at 11:51
  • no actually my application was hangs up when testing. so i want to take the job started screen calculation value and put it into the job completed screen value.@twntee – Ramesh Sep 09 '13 at 11:57
  • @user2738655 the hangup may be due to some other reason still, I have edited your JobStartScreen http://hastebin.com/hajujagowi.avrasm and JobCompletedScreen http://hastebin.com/banenepeda.avrasm, see if it works. – Rachit Mishra Sep 09 '13 at 11:59
  • i dont know why the application was hangs up while testing. also i have done two calculations in the above activity. for if else statement. is that correct – Ramesh Sep 09 '13 at 12:01
  • @user2738655 sorry bro, i cant verify your algorithm as it is completely your logic, see the logcat if you application hangs , might be we have some clue there – Rachit Mishra Sep 09 '13 at 12:04
  • k fine. it works well in emulator not in real device. anyway thanks. @twntee – Ramesh Sep 09 '13 at 12:07
  • can you give me your mail id. i will send my apk file. go through it. – Ramesh Sep 09 '13 at 12:10
  • @Ramesh twntee@gmail.com – Rachit Mishra Sep 09 '13 at 12:15
0

Use this code in finish button click

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

        }
    });

to get value from the sharepreference use the code given below in jobcompleted class

   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);
Nithinlal
  • 4,845
  • 1
  • 29
  • 40