0

我有一个带有按钮的活动。在 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 { private Claimant currentClaimant = null; TextView screenHeading = null; 私有字符串 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;
}

}

4

2 回答 2

0

在 android 中,您不能从另一个线程更新 UI。这是 android 开发中的限制(我认为这是删除不必要的错误的功能)。您可以为此使用 AsyncTask ...

在使用中,您可以在 donInBackground() 中执行长任务,然后可以使用 onProgressUpdate() 更新 UI ...请参阅此处的 AsyncTask 示例

编辑

有关更多信息,请参阅类似问题...

于 2013-02-25T13:21:29.727 回答
0

查看您的日志,我意识到您几乎每 100 毫秒更新一次 TextView,这就是它没有显示任何内容的原因。

这就是我模拟您正在做的事情的方式,并且我的 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 周期。

于 2015-11-23T17:16:51.273 回答