2013-09-26 8 views
1

내 앱에서 미디어 버튼을 누른 시간을 측정하고 싶습니다. 나는 미디어 버튼을 눌러에 수신 대기 브로드 캐스트 리시버를 등록 : (I 초보자 나처럼 바보 같은 실수를 용서하십시오 ...)MEDIA_BUTTON 버튼 이벤트 ACTION_DOWN 및 ACTION_UP이 동시에 수신되었습니다.

<receiver android:name="MyRec"> 
    <intent-filter> 
     <action android:name="android.intent.action.MEDIA_BUTTON"> 
     <action android:name="android.intent.action.MEDIA_BUTTON"/> 
     </action> 
    </intent-filter> 
</receiver> 

는 브로드 캐스트 리시버는 활동 (이상적이지의 방법을 활성화하지만, 이것은 단지입니다 테스트 목적)

public void onReceive(Context context, Intent intent) { 
    KeyEvent Xevent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);  
    MainActivity inst = MainActivity.instance(); 
    if ((Xevent.getAction() == KeyEvent.ACTION_DOWN)){ 
     inst.startTimer(); 
     } 
    else if ((Xevent.getAction() == KeyEvent.ACTION_UP)) { 
     inst.stopTimer(); 
    } 
} 

활동은 DIFF를가 startTimer()가 호출 시스템 시간을 소요하고 호출하는 stopTimer가 다시 때 보여

public void startTimer(){ 
    pressTime = System.currentTimeMillis(); 
} 

public void stopTimer(){ 
    pressDuration = System.currentTimeMillis() - pressTime; 
    TextView timerTextView = (TextView)findViewById(R.id.timerText); 
    timerTextView.setText(Long.toString(pressDuration)); 
} 

ISS의 ue는 두 이벤트가 동시에 호출되는 것을 보았습니다. 버튼을 놓을 때도 결국 타이머를 매우 짧은 시간 (몇 밀리 초)으로 계산합니다.이 시간은 내가 누르는 시간과 관련이 없습니다. 단추.

내가 뭘 잘못하고 있니?

답변

1

자신의 타이머를 사용할 필요가 없습니다. KeyEvent.ACTION_UP 작업을 수신 할 때 event 매개 변수의 getDownTimegetEventTime 메서드를 사용할 수 있습니다. 매니페스트에서 또한

, 중첩 된 <action android:name="android.intent.action.MEDIA_BUTTON"/> 당신은 여기에 설명 된 시간을 측정하기위한 솔루션을 찾을 수 있습니다

+0

필요하지 않습니다 : http://stackoverflow.com/a/39413753/1386969은 (빌드 API 레벨 21에서 근무 .VERSION_CODES.LOLLIPOP) – lidox