2013-02-25 10 views
0

我有一個按鈕的活動。在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; 
} 

}

+0

閱讀[RunOnUiThread的文檔](http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable))。更好地使用Handler的代替... – Pallavi 2013-02-25 13:31:52

+0

最有可能你有一個初始化'timerTextView'的錯誤,但我不確定沒有看到更多的代碼。 – Budius 2013-02-25 13:42:34

+0

你在哪裏初始化textView? runOnUiThread在底層使用處理程序,所以,使用處理程序和此方法之間沒有區別(回答上面的註釋)。 – tundundun 2013-02-25 14:19:10

回答

0

在Android中,您無法更新從另一個線程的用戶界面。這是在android開發中的限制(我認爲這是一個刪除不必要的bug的功能)。您可以使用的AsyncTask這個...

在使用時,可以執行內部donInBackground(長期的任務),比UI可以使用onProgressUpdate()進行更新...看到的AsyncTask例如here

編輯

更多信息請參見this類似的問題...

+0

我正在使用runOnUiThread(new Runnable())更新UI線程上的UI,如您在updateTime()方法中所見。它工作正常,當我第一次創建活動,但在相同的應用程序會話中第二次訪問相同的活動的時間值保持不變,作爲 公共無效運行(){ timerTextView.setText(time); Log.d(「更新時間線程」,「當前時間:」+時間); } 代碼每次運行時,我從後臺線程更改時間。 – 2013-02-27 07:51:02

0

展望你的日誌我知道要更新的TextView幾乎每100ms,這就是爲什麼它沒有顯示幹啥G。

這是我如何模擬你在做什麼,我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的循環。

相關問題