0

I have an activity with a button. On ACTION_DOWN I start recording and keep on recording while pressing this button. On ACTION_UP I have to stop recording. Recording thread is separate one. There is a text view on the activity that shows the recording time. Default value for time is “00:00:00:000” From recording thread i m calling updateTime method of the activity class:

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);
        }
    });
}

When I call this activity first time it work fine and update the time value properly. But if I select the same option from previous screen to start recording second time, the value of time text view remains same as “00:00:00:000” where as in second Log.d code line showing the proper value. Here is the 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

public class VerficationPBActivity extends Activity implements VoiceManagerListener, OnTouchListener { private Claimant currentClaimant = null; TextView screenHeading = null; private String screenHeadingText = null; TextView textToSpeakTextView = null; TextView timerTextView = null; TextView statusHintTextView = null; private Button 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;
}

}

  • read the [documentation for RunOnUiThread](http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)) . Better use Handler's instead... – PrincessLeiha Feb 25 '13 at 13:31
  • most likely you're having a error initialising `timerTextView` but I wouldn't know for sure without seeing more of your code. – Budius Feb 25 '13 at 13:42
  • Where do you initialize you textView? runOnUiThread uses handler under the hood, so, there no difference between using handler and this method (answer to the comment above). – tundundun Feb 25 '13 at 14:19
  • I have added my activity class code in my original question – Sajjad Awan Feb 27 '13 at 08:04

2 Answers2

0

In android, You can not update the UI from another thread. This is the restriction (which I consider a feature to remove unncecessary bugs) in android development. You can use AsyncTask for this...

In use, you can perform long task inside donInBackground() and than UI can be updated using onProgressUpdate()... see the AsyncTask example here

EDIT

see this similar question for more information...

Community
  • 1
  • 1
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
  • I am updating UI on UI thread using runOnUiThread(new Runnable()) as you can see in my updateTime() method. it s working fine when i create activity first time but accessing the same activity second time in same application session the time value remains same where as public void run() { timerTextView.setText(time); Log.d("UPDATE TIME Thread", "Current time:"+time); } code runs every time when i change the time from background thread. – Sajjad Awan Feb 27 '13 at 07:51
0

Looking into your logs I realize you are updating TextView almost every 100ms that's why its not showing anything.

This is how I simulated what you are doing and my textview displayed final value after 100th iteration:-

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();
    }

Try to update UI every second you might be eating CPU cycles as well.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154