In this post i'll going to share the problem when implementing chronometer in android application
Problem: I have one activity with three button START ,STOP and NEXT button.as name implies what they does so i don't need to explain about it to you.
i want to start second activity with the time as previous activity cover.It's one of toughest problem i have faced when try to implement this concept. so guys you don't need to panic i have attach code below. so before using chronometer you must have knowledge of SystemClock.
Note 1:
To start the chronometer you should use the setBase() method of chronometer with SystemClock.elapsedRealTime().
mChronometer.setBase(SystemClock.elapsedRealTime());
Note 2:
To start chronometer at a specific time.you have to convert d time in millisecond.
At 3 minutes
mChronometer.setBase(SystemClock.elapsedRealTime()-3*60*1000);
*******FIRST ACTIVITY********
Chronometer mTimer;
int current_chronometer_state = 0;
long mStopWatchTimeSTS=0;
STEP 1: Within onCreate() method, i am using bundle to get time from second activity
Bundle bundle = getIntent().getExtras();
if (bundle!= null)
mStopWatchTimeSTS = bundle.getLong("STOP_WATCH_TIME");
mTimer.setBase(SystemClock.elapsedRealtime()+mStopWatchTimeSTS);
} else {
mTimer.setBase(SystemClock.elapsedRealtime()+ mStopWatchTimeSTS);
}
STEP 2: Code for START button
mTimer.setBase(SystemClock.elapsedRealtime()+ mStopWatchTimeSTS);
mTimer.start();
current_chronometer_state = 1;
STEP 3: Code for STOP button
mTimer.stop(); 00:20
mStopWatchTimeSTS = mTimer.getBase() - SystemClock.elapsedRealtime();
current_chronometer_state = 0;
STEP 4: Code for NEXT button
if(current_chronometer_state == 1) {
mTimer.stop();
mStopWatchTimeSTS = mTimer.getBase() - SystemClock.elapsedRealtime();
current_chronometer_state = 0;
}
Intent intent = new Intent(this, SecondTestSheet.class);
Bundle bundle = new Bundle();
bundle.putString("ACTUAL_TIME", mActualTime);
bundle.putLong("STOP_WATCH",mStopWatchTimeSTS);
intent.putExtras(bundle);
startActivity(intent);
SECOND ACTIVITY
STEP 1: Within onCreate() method, i am using bundle to get time from first activity
long mPrevStopWatchTime=0;
int current_chronometer_state=0;
Note:get data from bundle
Bundle mExtra = getIntent().getExtras();
if (mExtra != null) {
mPrevStopWatchTime = mExtra.getLong("STOP_WATCH");
sActualTime = mExtra.getString("ACTUAL_TIME");
mChronometer.setBase(SystemClock.elapsedRealtime()+mPrevStopWatchTime);
}else{
mChronometer.setBase(SystemClock.elapsedRealtime()+mPrevStopWatchTime);
}
STEP 2: Code for START button
mChronometer.start();
mChronometer.setBase(SystemClock.elapsedRealtime()+mPrevStopWatchTime);
current_chronometer_state=1;
STEP 3: Code for STOP button
mChronometer.stop();
mPrevStopWatchTime=mChronometer.getBase()-SystemClock.elapsedRealtime();
current_chronometer_state=0;
STEP 4: Code for BACKbutton
if(current_chronometer_state==1){
mChronometer.stop();
mPrevStopWatchTime=mChronometer.getBase()-SystemClock.elapsedRealtime();
current_chronometer_state=0;
}
mChronometer.stop();
Intent intent=new Intent(context,FirstTestSheet.class);
Bundle bundle=new Bundle();
bundle.putLong("STOP_WATCH_TIME",mPrevStopWatchTime);
intent.putExtras(bundle);
startActivity(intent);
Chronometer Link
Problem: I have one activity with three button START ,STOP and NEXT button.as name implies what they does so i don't need to explain about it to you.
i want to start second activity with the time as previous activity cover.It's one of toughest problem i have faced when try to implement this concept. so guys you don't need to panic i have attach code below. so before using chronometer you must have knowledge of SystemClock.
Note 1:
To start the chronometer you should use the setBase() method of chronometer with SystemClock.elapsedRealTime().
mChronometer.setBase(SystemClock.elapsedRealTime());
Note 2:
To start chronometer at a specific time.you have to convert d time in millisecond.
At 3 minutes
mChronometer.setBase(SystemClock.elapsedRealTime()-3*60*1000);
*******FIRST ACTIVITY********
Chronometer mTimer;
int current_chronometer_state = 0;
long mStopWatchTimeSTS=0;
Bundle bundle = getIntent().getExtras();
if (bundle!= null)
mStopWatchTimeSTS = bundle.getLong("STOP_WATCH_TIME");
mTimer.setBase(SystemClock.elapsedRealtime()+mStopWatchTimeSTS);
} else {
mTimer.setBase(SystemClock.elapsedRealtime()+ mStopWatchTimeSTS);
}
STEP 2: Code for START button
mTimer.setBase(SystemClock.elapsedRealtime()+ mStopWatchTimeSTS);
mTimer.start();
current_chronometer_state = 1;
STEP 3: Code for STOP button
mTimer.stop(); 00:20
mStopWatchTimeSTS = mTimer.getBase() - SystemClock.elapsedRealtime();
current_chronometer_state = 0;
STEP 4: Code for NEXT button
if(current_chronometer_state == 1) {
mTimer.stop();
mStopWatchTimeSTS = mTimer.getBase() - SystemClock.elapsedRealtime();
current_chronometer_state = 0;
}
Intent intent = new Intent(this, SecondTestSheet.class);
Bundle bundle = new Bundle();
bundle.putString("ACTUAL_TIME", mActualTime);
bundle.putLong("STOP_WATCH",mStopWatchTimeSTS);
intent.putExtras(bundle);
startActivity(intent);
SECOND ACTIVITY
STEP 1: Within onCreate() method, i am using bundle to get time from first activity
int current_chronometer_state=0;
Note:get data from bundle
Bundle mExtra = getIntent().getExtras();
if (mExtra != null) {
mPrevStopWatchTime = mExtra.getLong("STOP_WATCH");
sActualTime = mExtra.getString("ACTUAL_TIME");
mChronometer.setBase(SystemClock.elapsedRealtime()+mPrevStopWatchTime);
}else{
mChronometer.setBase(SystemClock.elapsedRealtime()+mPrevStopWatchTime);
}
STEP 2: Code for START button
mChronometer.start();
mChronometer.setBase(SystemClock.elapsedRealtime()+mPrevStopWatchTime);
current_chronometer_state=1;
STEP 3: Code for STOP button
mChronometer.stop();
mPrevStopWatchTime=mChronometer.getBase()-SystemClock.elapsedRealtime();
current_chronometer_state=0;
STEP 4: Code for BACKbutton
if(current_chronometer_state==1){
mChronometer.stop();
mPrevStopWatchTime=mChronometer.getBase()-SystemClock.elapsedRealtime();
current_chronometer_state=0;
}
mChronometer.stop();
Intent intent=new Intent(context,FirstTestSheet.class);
Bundle bundle=new Bundle();
bundle.putLong("STOP_WATCH_TIME",mPrevStopWatchTime);
intent.putExtras(bundle);
startActivity(intent);
Chronometer Link
No comments:
Post a Comment