Code example for Chronometer

Methods: getBase, setOnChronometerTickListener, sleep

0
			Log.e(TAG, e.getMessage());
		} 
    } 
     
    public void clearChronoTickListener() { 
    	chrono.setOnChronometerTickListener(null);
    } 
     
    public void addChronoTickListener() { 
        chrono.setOnChronometerTickListener(new OnChronometerTickListener() {
        	long elapsedSeconds = 0;
        	 
        	@Override 
        	public void onChronometerTick(Chronometer chron) {
        		elapsedSeconds = (SystemClock.elapsedRealtime() - chrono.getBase()) / 1000;
				remainingTime.setText("Remaining time : " + (cutofTime-elapsedSeconds) + " seconds.");
				if (elapsedSeconds % cutofTime == 0) {
					// Say something on text to speech or play little mp3/wav 
					speakAndHear("Timer done. Do you need more time?", 3400, true);					 
        		} 
        	} 
        }); 
    } 
     
    @Override 
    public void onPause() { 
    	super.onPause(); 
    	// Save application state or clear state.   	 
    } 
     
    public void startVoiceRecognition() { 
    	Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    	startActivityForResult(intent, 0);	
    } 
     
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	boolean isTimeSet = false;
    	boolean isUnitsSet = false;
    	String units = "minutes";
    	int multiplier = 0;
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0 || resultCode == Activity.RESULT_OK) {
        	List<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        	for (String str : text) {
        		speechMsg.setText("You said : '" + str + "'");
				String[] parts = str.split(" ");
				for (String part : parts) {
					if (part.startsWith("more")) {
						speakAndHear("How much time?", 2300, true); 
						attempts = 0;
					} else if (part.startsWith("over")) {
						finishActivity(); 
					} else { 
						try { 
							cutofTime = Integer.parseInt(part);
							isTimeSet = true;
						} catch (NumberFormatException nfe) {
							if (part.startsWith("hour")) {
								isUnitsSet = true;
								multiplier = 60 * 60;
								units = "hours";
							} else if (part.startsWith("min")) {
								isUnitsSet = true;
								multiplier = 60;
								units = "minutes";
							} else if (part.startsWith("sec")) {
								isUnitsSet = true;
								multiplier = 1;
								units = "seconds";
							} 
						} 
 
						if (isTimeSet && isUnitsSet) {
							attempts = 0;
							break; 
						} 
					} 
				} 
			} 
        	 
        	if(isTimeSet && isUnitsSet) {
        		speakAndHear("I'll remind you in " + cutofTime + " more " + units, 0, false);
        		cutofTime = cutofTime*multiplier;
        		addChronoTickListener(); 
        	} else { 
        		if(attempts < MAX_ATTEMPTS) {
        			speakAndHear("Couldn't hear you. Can you tell me again?", 3000, true); 
        		} else { 
        			finishActivity(); 
        		} 
        		attempts++;
        	} 
        } 
    } 
     
    private void finishActivity() { 
    	clearChronoTickListener(); 
    	speakAndHear("Finishing activity.", 0, false); 
    } 
     
    private void sleep(long millis) {
    	try { 
			Thread.sleep(millis);
		} catch (Exception e) {
			// Do nothing. 
		} 
    }