Suraj Bahadur

Suraj Bahadur
  • Home
  • Features
  • _Multi DropDown
  • __DropDown 1
  • __DropDown 2
  • __DropDown 3
  • _ShortCodes
  • _Sitemap
  • _Error Page
  • Seo Services
  • Documentation
  • Download This Template

How To Implement Chronometer In Android

November 26, 2016
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
Share This:
Facebook Twitter Google+ Pinterest Linkedin Whatsapp
at November 26, 2016
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Popular

  • Way To Find  Serial Key Of Any Software Using Simple Newbie Trick
    Way To Find Serial Key Of Any Software Using Simple Newbie Trick
    Hi Newbie's ,  In this post i'm gonna give you a newbie tips and tricks to find any sofware serial key by using simple newbie tri...
  • [How to] Logic Behind To Print Pattern In Any language Using The Concept Of For Loop
    [How to] Logic Behind To Print Pattern In Any language Using The Concept Of For Loop
    Hi all newbie's , here in this tutorial i'm going to show a simple logic behind to print pattern using for  in any programming lang...
  • Part -2 :How To Print Diamond in C++
    #include using namespace std; int main(){ int i,j,k,row; row=3; for( k=1;k<=row;k++) { for(j=1;j<=3-k;j++){ cout<<...
  • How To Implement Chronometer In Android
    In this post i'll going to share the problem when implementing chronometer in android application Problem: I have one activity with t...
  • How to print diamond pattern in c++ & c...
    Following is the program to print diamond using for loop in any programming language. #include<iostream> using namespace std; ...

Tags

batch programming newbie tips and tricks Programming Logic To Print Diamond Pattern In C and C++ .......n so on. protect folder with password SHA1 Fingerprint cerification in android through Cmd Top 54 Hidden list of proxy sites trick

Search This Blog

Suraj Bahadur | 2016 |. Powered by Blogger.

Content

  • ▼  2016 (16)
    • ►  December (1)
    • ▼  November (1)
      • How To Implement Chronometer In Android
    • ►  October (7)
    • ►  September (7)

Suraj Bahadur

Suraj Bahadur

Report Abuse

Follow @surajbahadur1

About Me

My photo
Suraj Bahadur
View my complete profile
Suraj D NeWbie | 

Linkedin

Suraj Bahadur

Instagram

Subscribe me

Posts
Atom
Posts
Comments
Atom
Comments

Visitors

suraj

  • Facebook
  • Twitter
  • LinkedIn
Created By SoraTemplates | Distributed By Blogger Templates