在Activity A
我正在播放一首歌,现在又转到Activity B
这里再一个按钮播放另一首歌
现在,当我在活动B时,我必须检查Activity A's
歌曲already playing
是否必须向用户显示已在活动A上播放歌曲的消息,如果用户未在活动A上播放歌曲则用户可以开始在Activity B
ActivityB.java: -
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(ActivityA.mPlayer!=null && ActivityA.mPlayer.isPlaying()) // getting exception
{
Toast.makeText(Activity.this, "Already playing song on Activity A", Toast.LENGTH_LONG).show();
}
else
{
mediaPlayer.start();
btnPlay.setVisibility(View.GONE);
btnPause.setVisibility(View.VISIBLE);
}
}
});
现在当我getting
这个Exception
时:就像我在活动playing song
中开始A
,现在转到活动B
,这里我{ {1}}按钮 - 它显示我的消息tapped on play
然后我决定回到活动A,我点击停顿现在我没有在活动A中播放歌曲,我再次移动到活动B(这时我点击"Already playing song on Activity A"
按钮获取Play
)在此行:
IllegalStateException
if(ActivityA.mPlayer!=null && ActivityA.mPlayer.isPlaying())
答案 0 :(得分:1)
我认为最好在ActivityA
:
Intent i = new Intent(this, ActivityB.class);
i.putExtra("isPlaying", mPlayer.isPlaying);
startActivity(i);
在ActivityB's
onCreate
:
Bundle data = getIntent().getExtras();
if(data != null)
{
isPlayingBefore = data.getParcelable("isPlaying");
}
在onClick
上:
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(isPlayingBefore)
{
Toast.makeText(Activity.this, "Already playing song on Activity A", Toast.LENGTH_LONG).show();
}
else
{
mediaPlayer.start();
btnPlay.setVisibility(View.GONE);
btnPause.setVisibility(View.VISIBLE);
}
}