2010-07-12 4 views
0

아래 기능은 Google 카메라 앱 코드의 일부입니다. 녹음 시작 후 경과 된 시간을 동적으로 표시하는 텍스트보기를 업데이트해야합니다. 하지만이 함수는 루프가 없으므로 어떻게 동작합니까? 도와주세요.은 Google의 캠코더 앱 업데이트 타이머를 해석하는 데 도움이 필요합니다.

개인 정적 final int UPDATE_RECORD_TIME = 5;

private final Handler mHandler = new MainHandler(); 

private class MainHandler extends Handler { 
     @Override 
     public void handleMessage(Message msg) { 

      switch (msg.what) { 

       case UPDATE_RECORD_TIME: { 
        updateRecordingTime(); 
        break; 
       } 

       default: 
        Log.v(TAG, "Unhandled message: " + msg.what); 
        break; 
      } 
     } 
    } 

int 초 = intent.getIntExtra (MediaStore.EXTRA_DURATION_LIMIT, 0); mMaxVideoDurationInMs = 1000 * 초;

mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs); 

//this function is to update the recording time 

private void updateRecordingTime() { 

if (!mMediaRecorderRecording) { return; } 
long now = SystemClock.uptimeMillis(); 
long delta = now - mRecordingStartTime; 

// Starting a minute before reaching the max duration 
// limit, we'll countdown the remaining time instead. 
boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 
     && delta >= mMaxVideoDurationInMs - 60000); 

long next_update_delay = 1000 - (delta % 1000); 
long seconds; 
if (countdownRemainingTime) { 
    delta = Math.max(0, mMaxVideoDurationInMs - delta); 
    seconds = (delta + 999)/1000; 
} else { 
    seconds = delta/1000; // round to nearest 
} 

long minutes = seconds/60; 
long hours = minutes/60; 
long remainderMinutes = minutes - (hours * 60); 
long remainderSeconds = seconds - (minutes * 60); 

String secondsString = Long.toString(remainderSeconds); 
if (secondsString.length() < 2) { 
    secondsString = "0" + secondsString; 
} 
String minutesString = Long.toString(remainderMinutes); 
if (minutesString.length() < 2) { 
    minutesString = "0" + minutesString; 
} 
String text = minutesString + ":" + secondsString; 
if (hours > 0) { 
    String hoursString = Long.toString(hours); 
    if (hoursString.length() < 2) { 
     hoursString = "0" + hoursString; 
    } 
    text = hoursString + ":" + text; 
} 
mRecordingTimeView.setText(text); 

if (mRecordingTimeCountsDown != countdownRemainingTime) { 

    // Avoid setting the color on every update, do it only 
    // when it needs changing. 
    mRecordingTimeCountsDown = countdownRemainingTime; 

    int color = getResources().getColor(countdownRemainingTime 
      ? R.color.recording_time_remaining_text 
      : R.color.recording_time_elapsed_text); 

    mRecordingTimeView.setTextColor(color); 
} 

// Work around a limitation of the T-Mobile G1: The T-Mobile 
// hardware blitter can't pixel-accurately scale and clip at the 
// same time, and the SurfaceFlinger doesn't attempt to work around 
// this limitation. In order to avoid visual corruption we must 
// manually refresh the entire surface view when changing any 
// overlapping view's contents. 

mVideoPreview.invalidate(); 
mHandler.sendEmptyMessageDelayed( 
     UPDATE_RECORD_TIME, next_update_delay); 
} 

답변

0
mHandler.sendEmptyMessageDelayed(UPDATE_RECORD_TIME, next_update_delay); 

이 라인은 시간 범위 next_update_delay 후에 이벤트를 송신한다. 아마도 UPDATE_RECORD_TIME 이벤트를 처리하는 어딘가와 mHandler이라는 선언이 있습니다. 나는 updateRecordingTime으로 전화를 걸어 일정 시간이 지나면 메시지를 보냈을 것입니다.

이것은 루프의 비동기 버전입니다. 이 방법은 작업을 수행 한 다음 어느 정도 시간이 지나면 실행되도록 일정을 계획합니다.

그러나 처음에는 루프를 시작하려면 updateRecordingTime 또는 updateRecordingTime 외부의 sendMessageDelayed 메서드에 대한 초기 호출이 있어야합니다.

+0

은 위의 코드가 작동하도록하기 위해 다음 코드 조각입니까? 비공개 정적 final int UPDATE_RECORD_TIME = 5; \t \t 개인 최종 처리기 mHandler = new MainHandler(); \t \t 개인 클래스 MainHandler 핸들러를 확장 { \t @Override \t 공개 무효 handleMessage (메시지 MSG) { \t 스위치 (msg.what) { \t 케이스 UPDATE_RECORD_TIME { \t updateRecordingTime(); \t 중단; \t} \t 기본값 : Log.v (TAG, "처리되지 않은 메시지 :"+ msg.what); 단절; } \t} \t} – Namratha

+0

더 명확한보기를 위해 편집 된 코드에 추가했습니다. – Namratha

+0

해야합니다. 그러나 루프를 시작하려면'updateRecordingTime'을 한 번 호출해야하거나 Activity 코드에서 한번 UPDATE_RECORD_TIME 메시지를 보내야한다는 것을 기억하십시오. –

관련 문제