2011-03-10 11 views
2

私は1秒に1回TextViewを更新しようとしています。具体的には、曲のタイマーです。私はこれを実装するためにハンドラを使用しています。それは正しく設定されているようです - 私は例外がありませんし、私はデバッグ時に間違って何かが間違って表示されませんが、静かにTextViewを更新することができません。それはちょうど0時にとどまります。ハンドラを使用して毎秒TextViewを更新できません

private TextView currentTime; 
private int startTime = 0; 
private Handler timeHandler = new Handler(); 
private Runnable updateTime = new Runnable() { 
    public void run() { 
     final int start = startTime; 
     int millis = appService.getSongPosition() - start; 
     int seconds = (int) ((millis/1000) % 60); 
     int minutes = (int) ((millis/1000)/60); 
     Log.d("seconds",Integer.toString(seconds)); // looks okay, prints new value every second 
     if (seconds < 10) { 
      // this is hit, yet the textview is never updated 
      currentTime.setText(String.format("%d:0%d",minutes,seconds)); 
     } else { 
      currentTime.setText(String.format("%d:%d",minutes,seconds)); 
     } 
     timeHandler.postAtTime(this,start+(((minutes*60)+seconds+1)*1000)); 
    } 
}; 

private ServiceConnection onService = new ServiceConnection() { 
public void onServiceConnected(ComponentName className, 
     IBinder rawBinder) { 
    appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer 

    // start playing the song, etc... 

    if (startTime == 0) { 
     startTime = appService.getSongPosition(); 
     timeHandler.removeCallbacks(updateTime); 
     timeHandler.postDelayed(updateTime,1000); 
    } 
} 

public void onServiceDisconnected(ComponentName classname) { 
    appService = null; 
} 
}; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.song); 

    currentTime = (TextView) findViewById(R.id.current_time); 

    // ... 

    bindIntent = new Intent(Song.this,MPService.class); 
    bindService(bindIntent,onService,0); 
} 

答えて

関連する問題