我有一个带按钮的活动。在ACTION_DOWN上,我开始录制并在按下此按钮的同时继续录制。在ACTION_UP上,我必须停止录制。录音线程是独立的。有一个关于活动的文本视图,显示录制时间。时间的默认值为"00:00:00:000"从记录线程我调用活动类的updateTime方法:
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扩展Activity实现VoiceManagerListener、OnTouchListener{私人索赔人current索赔人=无效;TextView屏幕标题=null;private字符串screenHeadingText=null;TextView textToSpeakTextView=null;TextView timerTextView=null;TextView状态提示TextView=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;
}
}
在android中,您不能从另一个线程更新UI。这是android开发中的限制(我认为这是一个删除不必要错误的功能(。您可以对此使用AsyncTask。。。
在使用中,您可以在donInBackground((中执行长任务,然后使用onProgressUpdate((更新UI。。。请参阅此处的AsyncTask示例
编辑
有关更多信息,请参阅此类似问题。。。
查看日志,我意识到您几乎每100毫秒更新一次TextView,这就是为什么它没有显示任何内容。
这就是我模拟你正在做的事情的方式,我的文本视图在第100次迭代后显示了最终值:-
for(int i=0;i<100;i++)
{
((TextView) findViewById(R.id.timerTextView)).setText(""
+ System.currentTimeMillis());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
试着每秒钟更新一次UI,你可能也会消耗CPU周期。