0
我有一個seekbar,我需要從服務中更新。但是光標根本不移動。 下面是代碼(你可以看到很多的佈局,因爲我把你需要的代碼,但也有在這些佈局其他視圖對象):使用佈局壓縮器更新seekbar
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="-80dp"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<SeekBar
android:id="@+id/seekBarTime"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
的Java
public class MediaPlayerService extends Service implements Runnable {
private MediaPlayer mediaPlayer = null;
private SeekBar seekBarTime;
private Handler handler;
private Runnable runnable;
@Override
public void onCreate() {
//SET SEEKBAR
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.play_song , null);
seekBarTime = (SeekBar) layout.findViewById(R.id.seekBarTime);
seekBarTime.setOnSeekBarChangeListener(this);
handler = new Handler();
runnable = this;
handler.post(runnable);
}
// Seekbar
public void run() {
if(mediaPlayer.isPlaying()) {
int currentPosition = 0;
int total = mediaPlayer.getDuration();
seekBarTime.setMax(total);
currentPosition = mediaPlayer.getCurrentPosition();
Log.w("disgustingapps", currentPosition + "/" + total);
seekBarTime.setProgress(currentPosition);
}
handler.postDelayed(runnable, 1000);
}
}
的logcat的向我保證currentPosition和total不是空的。然而沒有發生。 你能幫我嗎? 在此先感謝
我敢肯定,它實際上是在玩logcat的確認。但是,你把代碼放在服務中還是放在活動中? – user1315621
在活動中。 –