我想通过附加意图传递给Servive:
ArrayList<String> playlistStr;
int current;
现在看起来就像这样:
MainActivity.class
@Override
protected void onResume() {
super.onResume();
this.overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top);
if (getIntent().hasExtra("playlist")) {
playlistStr = (ArrayList<String>) (getIntent().getExtras().get("playlist"));
playlist = new Playlist(playlistStr);
}
if (!playlist.isEmpty()) {
playlistView.setVisibility(View.VISIBLE);
playlistView.setVisibleItems(5);
playlistView.setViewAdapter(new PlaylistViewAdapter(this));
try {
updatePlaylist(playlistView, playlist, playlistView.getCurrentItem());
if (mBound){
unbindService(mConnection);
mBound = false;
}
//BINDING SERVICE
Intent intent = new Intent(this, MediaPlayerManager.class);
intent.putExtra("playlistStr",playlistStr);
intent.putExtra("current",current);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
} catch (IOException e) {
e.printStackTrace();
}
playlistView.addClickingListener(new OnWheelClickedListener() {
@Override
public void onItemClicked(WheelView wheel, int itemIndex) {
prev = playlistView.getCurrentItem();
playlistView.setCurrentItem(itemIndex);
playlistView.scroll(itemIndex - playlistView.getCurrentItem(), 0);
current = playlistView.getCurrentItem();
try {
mService.start(current);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
MediaPlayerManager.class
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(getApplicationContext(), "onBind", Toast.LENGTH_SHORT).show();
if (intent.hasExtra("playlistStr")) {
playlistStr = (ArrayList<String>) intent.getExtras().get("playlistStr");
playlist = new Playlist(playlistStr);
if (!playlist.isEmpty())
song = MediaPlayer.create(context, Uri.parse(playlist.get(0)));
}
if (intent.hasExtra("current"))
current = intent.getIntExtra("current",-1);
return mBinder;
}
在MainActivity.class中playlistStr和current都有正确的值(playlistStr不为空,当前&gt;为0)。但是在服务中播放playlistStr = []和当前= 0.哪里可能有问题?
答案 0 :(得分:0)
我不确定您是否需要playlist
对象,因为您可能只使用playlistStr
中列出的字符串
您也可以明确使用Intent.getSerializableExtra
playlistStr = (ArrayList<String>) intent.getSerializableExtra("playlistStr");
//playlist = new Playlist(playlistStr);
if (!playlistStr.isEmpty())
song = MediaPlayer.create(context, Uri.parse(playlistStr.get(0)));