我有一個按鈕的活動。在ACTION_DOWN上,我開始錄製並按住此按鈕繼續錄製。在ACTION_UP上,我必須停止錄製。錄製線程是獨立的。有關顯示錄製時間的活動的文本視圖。時間 默認值是「00:00:00:000」 從記錄線程IM調用活動類的錄入方法:爲什麼即使在UI線程上TextView的文本值也沒有被改變
public void updateTime(final String time) {
Log.d("***UPDATE TIME Thread", "Current time:" + time);
runOnUiThread(new Runnable() {
public void run() {
timerTextView.setText(time);
Log.d("UPDATE TIME Thread", "Current time:"+time);
}
});
}
當我把這個活動第一次做工精細和更新的時間價值正常。但是如果我從前一個屏幕選擇相同的選項以開始第二次錄製,則時間文本視圖的值將保持與「00:00:00:000」相同,如第二個Log.d代碼行中顯示的正確值。這裏是logcat的
02-25 16:07:10.660: D/Recording(5075): Start
02-25 16:07:10.695: D/***UPDATE TIME Thread(5075): Current time:00:00:00:032
02-25 16:07:10.711: D/UPDATE TIME Thread(5075): Current time:00:00:00:032
02-25 16:07:10.843: D/***UPDATE TIME Thread(5075): Current time:00:00:00:174
02-25 16:07:10.863: D/UPDATE TIME Thread(5075): Current time:00:00:00:174
02-25 16:07:10.945: D/***UPDATE TIME Thread(5075): Current time:00:00:00:280
02-25 16:07:10.972: D/UPDATE TIME Thread(5075): Current time:00:00:00:280
02-25 16:07:11.082: D/***UPDATE TIME Thread(5075): Current time:00:00:00:418
02-25 16:07:11.089: D/UPDATE TIME Thread(5075): Current time:00:00:00:418
02-25 16:07:11.203: D/***UPDATE TIME Thread(5075): Current time:00:00:00:535
02-25 16:07:11.222: D/UPDATE TIME Thread(5075): Current time:00:00:00:535
02-25 16:07:11.351: D/***UPDATE TIME Thread(5075): Current time:00:00:00:676
......
Here is my Activity Class Code
公共類VerficationPBActivity延伸活動實現VoiceManagerListener,OnTouchListener { 私人索賠人currentClaimant = NULL; TextView screenHeading = null; private String screenHeadingText = null; TextView textToSpeakTextView = null; TextView timerTextView = null; TextView statusHintTextView = null; 私人按鈕pushToRecordButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verfication_pb);
ActivityController.getActivityController().setCurrentActivity(this);
screenHeading = (TextView) findViewById(R.id.screenHeading);
textToSpeakTextView = (TextView) findViewById(R.id.text_to_speek);
timerTextView = (TextView) findViewById(R.id.timer);
statusHintTextView = (TextView) findViewById(R.id.status_hint);
screenHeadingText = (String) getIntent().getSerializableExtra("heading");
currentClaimant = (Claimant) getIntent().getSerializableExtra("claimant");
screenHeading.setText(screenHeadingText);
if (currentClaimant != null) {
textToSpeakTextView.setText(currentClaimant.getNextPrompt());
statusHintTextView.setText(currentClaimant.getStatusHint());
}
pushToRecordButton = (Button) findViewById(R.id.pushToRecord);
pushToRecordButton.setOnTouchListener(this);
}
@Override
public void updateTime(final String time) {
Log.d("***UPDATE TIME Thread", "Current time:" + time);
runOnUiThread(new Runnable() {
public void run() {
timerTextView.setText(time);
Log.d("UPDATE TIME Thread", "Current time:"+time);
Log.d("UPDATE TIME Thread", "Current value:"+timerTextView.getText());
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
VoiceManager.getInstance().startRecording();
break;
case MotionEvent.ACTION_UP:
Log.d("Push Button Down", "Up Event Fired");
VoiceManager.getInstance().stop();
String audioDataBase64 = VoiceManager.getInstance().getAudioDateinBase64Encoding();
if (screenHeadingText.equals("Verification")) {
VBSServiceManager.getInstance().processVerification(currentClaimant, audioDataBase64);
} else {
VBSServiceManager.getInstance().processEnrollment(currentClaimant, audioDataBase64);
}
break;
}
return false;
}
}
閱讀[RunOnUiThread的文檔](http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable))。更好地使用Handler的代替... – Pallavi 2013-02-25 13:31:52
最有可能你有一個初始化'timerTextView'的錯誤,但我不確定沒有看到更多的代碼。 – Budius 2013-02-25 13:42:34
你在哪裏初始化textView? runOnUiThread在底層使用處理程序,所以,使用處理程序和此方法之間沒有區別(回答上面的註釋)。 – tundundun 2013-02-25 14:19:10