Android:TextView已锁定以进行更新

时间:2014-04-20 12:07:09

标签: android multithreading textview

我有一个开始播放mp3文件的帖子,在这个帖子中我在往返后更新我的TextView我使用其他thred来更新我的播放器和我的TextView但是在这种情况下textview没有更新?!

public void updatePlayProgressUpdater() {
            SharedPreferences  sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            mediaLengthInSeconds = sharedPreferences.getLong("mediaLengthInSeconds", 0);            
            float mediaPositionSeconds = sharedPreferences.getFloat("mediaPositionSeconds", 0);
            float progress = (mediaPositionSeconds / mediaLengthInSeconds);
            sb.setProgress((int) (progress * 100));

            sb.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    seekChange(v);
                    return false;
                }
            });
            try {
                textStreamed
                .setText(convertDuration(mediaPlayer.getCurrentPosition() / 1000)
                        + "/" + convertDuration(mediaLengthInSeconds)); 
            } catch (Exception e) {
                Log.v("Exception","");
            }

                Runnable notification = new Runnable() {
                    public void run() {
                        updatePlayProgressUpdater();
                    }
                };
                handler.postDelayed(notification, 1000);
            }

...

Runnable notification = new Runnable() {
                    public void run() {
                        startPlayProgressUpdater();
                    }
                };

...

// comment ...


   public void startPlayProgressUpdater() {

            mediaLengthInSeconds = mediaPlayer.getDuration() / 1000;
            float mediaPositionSeconds = mediaPlayer.getCurrentPosition() / 1000;
            float progress = (mediaPositionSeconds / mediaLengthInSeconds);

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putLong("mediaLengthInSeconds", mediaLengthInSeconds);
            editor.putFloat("mediaPositionSeconds", mediaPositionSeconds);
            editor.commit();   // I missed to save the data to preference here,.

            sb.setProgress((int) (progress * 100));     
//          playButton.setImageResource(R.drawable.pause);
            sb.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    seekChange(v);
                    return false;
                }
            });
            textStreamed
                    .setText(convertDuration(mediaPlayer.getCurrentPosition() / 1000)
                            + "/" + convertDuration(mediaLengthInSeconds));
            if (mediaPlayer.isPlaying()) {
                Runnable notification = new Runnable() {
                    public void run() {
                        startPlayProgressUpdater();
                    }
                };
                handler.postDelayed(notification, 1000);
            }
        }

...

private void LoadPreferences(){
        SharedPreferences  sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        int  state = sharedPreferences.getInt("stateDB", 0);        
        sb.setProgress(state);
        textStreamed.setText(sharedPreferences.getString("statetextStreamed", ""));
        coursTitle.setText(sharedPreferences.getString("coursTitle", ""));
        coursIntervenant.setText(sharedPreferences.getString("coursIntervenant", ""));
        new Thread() {
              public void run() {

                      try {
                          runOnUiThread(new Runnable() {

                              @Override
                              public void run() {
                                  updatePlayProgressUpdater();
                              }
                          });
                          Thread.sleep(300);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
              }
          }.start();

       }

...

@Override
       protected void onResume() {      
           super.onResume();
           LoadPreferences();           
       }

2 个答案:

答案 0 :(得分:0)

line 525上将startPlayProgressUpdater();替换为:

            Runnable notification = new Runnable() {
                public void run() {
                    startPlayProgressUpdater();
                }
            };
            handler.postDelayed(notification, 1000);

答案 1 :(得分:0)

创建应用程序时,有一个主要线程(称为UI线程)可以处理所有UI更新。您创建的其他线程无法直接更新UI。听起来你有两种更新方法,一种是从UI线程调用,另一种是从后台线程调用。

有很多文章比我更好地解释它,例如http://developer.android.com/training/multiple-threads/communicate-ui.html