我正在为音乐播放器编写代码,除了ActionBar之外,一切似乎都运行正常。当一首歌正在播放时,一个线程每秒都会探测该服务以改变歌曲,如果歌曲发生变化,则处理程序对象会更改UI元素。
final Handler uiHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
song = data.getParcelable("song_info");
songText.setText(song.getSongTitle());
artistText.setText(song.getSongArtist());
seekBar.setMax(mService.getDuration()/1000); //All these work
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(mService.getSongName()); //Not updating
super.handleMessage(msg);
}
};
这样做的是当来自线程的消息时,它会更新UI。我可以确认这是有效的,并在我需要时调用。所有文本视图和搜索栏都会根据需要进行更新。但是,getSupportActionBar()。setTitle(...)不会更新标题栏。
如果我在同一个类的其他方法中执行此操作,则设置操作栏标题的方法相同。我在这里遗漏了什么吗?
提前谢谢!
的更新
这里有一些活动所拥有的代码,以防它有助于攻击。
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
MusicService.MusicBinder mBinder = (MusicService.MusicBinder)iBinder;
mService = mBinder.getService();
pauseBtn = (ImageButton)findViewById(R.id.pause);
seekBar.setMax(mService.getDuration()/1000);
CounterThread cThread = new CounterThread();
Thread counter = new Thread(cThread);
counter.start();
if(mService.isPaused()) {
pauseBtn.setImageResource(R.drawable.ic_play_arrow_white_36dp);
}
else {
pauseBtn.setImageResource(R.drawable.ic_pause_white_36dp);
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
private class CounterThread implements Runnable {
@Override
public void run() {
while(true) {
try {
if(!mService.getSongName().equals(song.getSongTitle())) //This is invoked when there is a change in the song.{
Message msg = uiHandler.obtainMessage();
Bundle b = new Bundle();
b.putParcelable("song_info", mService.getSong());
msg.setData(b); //I receive this message in the handler
uiHandler.sendMessage(msg);
}
seekBar.setProgress(mService.getCurrPosn()/1000);
Thread.sleep(1000);
} catch (Exception e){
e.printStackTrace();
}
}
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_playing);
song = getIntent().getParcelableExtra("song_info");
songText = (TextView) findViewById(R.id.songname);
artistText = (TextView) findViewById(R.id.artistname);
songText.setText(song.getSongTitle());
artistText.setText(song.getSongArtist());
header = getSupportActionBar();
seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
// Do nothing
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do nothing
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
mService.seek(progress*1000);
}
});
assert header != null;
header.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
header.setCustomView(R.layout.head_layout);
titleText = (TextView) findViewById(R.id.titleText);
titleText.setText(song.getSongTitle());
}
/*
More operations
*/
public void next(View v) {
mService.nextSong();
pauseBtn.setImageResource(R.drawable.ic_pause_white_36dp);
assert header != null;
header.setTitle(mService.getSongName()); // This works fine
songText.setText(mService.getSongName());
artistText.setText(mService.getArtistName());
titleText.setText(mService.getSongName());
seekBar.setMax(mService.getDuration()/1000);
}
不要介意轻微的缩进偏移。