我有 2 项活动:
活动 A - 列表视图/适配器
活动 B - 广播
在活动 A 中,我选择一个收音机,然后 B 播放该收音机(服务)。
但每次我在列表中选择另一个收音机时,活动 B 都会再次实例化,收音机会停止并再次播放。
情况示例:
# 1 - I'm playing Radio X, I choose X on the list
# 2 - A new instance is created (service is in onCreate() of Activity B)
# 3 - Radio X playing (play() is in onStart() of service)
# 4 - I go back to the list
# 5 - I want to play Radio Y
# 6 - A new instance is created (service is in onCreate() of Activity B)
# 7 - Radio Y playing (play() is in onStart() of service)
# * In onCreate() of service isn't doing nothing
一切都很好,但是如果我回到列表并选择相同的收音机会发生什么,例如:
# 1 - Radio Y playing
# 2 - I go back to the list
# 3 - I wanna go to Radio Y again
# 4 - A new instance is created (service is in onCreate() of Activity B) (I don't want this)
# 5 - Radio Y stops and plays again (I don't want this)
我想有一种方法来检查正在播放的收音机是否与我想播放的收音机相同,并且不要创建新实例,也不要停止并再次播放相同的收音机。
编辑:
列表显示
if (item == "Radio 1"){
Intent intent = new Intent(getBaseContext(), Radio.class);
intent.putExtra("radio", "http://test1.com");
this.startActivity(intent);
} else if (item == "Radio 2"){
Intent intent = new Intent(getBaseContext(), Radio.class);
intent.putExtra("radio", "http://test2.com");
this.startActivity(intent);
}
无线电.java
@Override
public void onCreate(Bundle icicle) {
requestWindowFeature(Window.FEATURE_LEFT_ICON);
super.onCreate(icicle);
setContentView(R.layout.main);
Intent music = new Intent(getApplicationContext(), Service.class);
music.putExtra("url", this.getIntent().getStringExtra("radio"));
startService(music);
}
服务.java
@Override
public void onStart(Intent intent, int startid) {
Multiplayer m = new MultiPlayer();
m.playAsync(intent.getExtras().getString("url"));
}