Reputation: 5980
Alright, so I've made a Service
so I can keep my music player working in all of my activities. At least, that was the plan.
I am able to bind to my service in some of my activities, but in other activities the binding doesn't seem to do anything.
Set up My service gets started on my MainScreen:
startService(new Intent(this, MusicService.class));
I'm trying to bind to it in a few other activities. The list of activities looks like this:
MainScreen -> FillerActivity -> bind to service -> Works
MainScreen -> AlbumList -> SongList -> SingleSong -> bind to service -> Does not work
What might be the reason for the binding working in the FillerActivity class, but not working in the SingleSong class?
My code is as following:
Filler
public class Filler extends Activity {
private MusicService s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filler);
doBindService();
Button b = (Button) findViewById(R.id.terug);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
public void startSong(View view){
if(s != null){
s.startSong(s.songPlaying);
System.out.println("Song started");
}
}
public void stopSong(View view){
if(s != null){
s.stopSong();
System.out.println("Song paused");
}
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
s = ((MusicService.MyBinder) binder).getService();
Toast.makeText(Filler.this, "Connected",
Toast.LENGTH_SHORT).show();
System.out.println("s is gevuld");
}
public void onServiceDisconnected(ComponentName className) {
s = null;
}
};
void doBindService() {
bindService(new Intent(this, MusicService.class), mConnection,
Context.BIND_AUTO_CREATE);
}
}
SingleSong
public class SingleSongInfo extends Activity {
private MediaDetail mDetails;
private TextView textView;
boolean playing = false;
private String songURL;
private MusicService s;
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album_song_info);
Bundle bun = getIntent().getExtras();
doBindService();
if (s != null) {
System.out.println("S filled");
} else {
System.out.println("S empty");
}
b = (Button) findViewById(R.id.playButtonSongPage);
mDetails = (MediaDetail) bun.getSerializable("details");
if (mDetails != null) {
System.out.println("Details gevuld");
songURL = mDetails.getSong_url();
setUpTextViews();
setUpImage();
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
}
});
}
}
public void buttonPressed(View view) {
if (s != null) {
if (!s.isPlaying()) {
s.startSong(songURL);
b.setBackgroundResource(R.drawable.button_pause);
} else {
s.stopSong();
b.setBackgroundResource(R.drawable.button_play);
}
} else {
System.out.println("S is leeg");
}
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
s = ((MusicService.MyBinder) binder).getService();
Toast.makeText(SingleSongInfo.this, "Connected", Toast.LENGTH_SHORT)
.show();
}
public void onServiceDisconnected(ComponentName className) {
s = null;
}
};
void doBindService() {
bindService(new Intent(this, MusicService.class), mConnection,
Context.BIND_AUTO_CREATE);
}
}
Any help is greatly appreciated.
An interesting discovery after doing some more testing.
When I call the SingleSong class directly from the MainScreen, the MediaService
object s
gets filled and is usable. It even prints a message that it has been filled.
However, when the SingleSong class gets opened through the SongList (which opens the SingleSong class in a tabview with some other classes), the object that not get filled. BUT, and this is where it get's strange, it does not print a message that the object has NOT been filled, even though it should. The code for this can be found below.
What might be causing the code from completely not being run in a tabView?
Code used:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
s = ((MusicService.MyBinder) binder).getService();
Toast.makeText(SingleSongInfo.this, "Connected", Toast.LENGTH_SHORT)
.show();
if (s != null) {
System.out.println("S filled"); // works when calling class directly
} else {
System.out.println("S empty"); // Does not print when object is empty when class is called through SongList (tabview)
}
}
public void onServiceDisconnected(ComponentName className) {
s = null;
}
};
Upvotes: 3
Views: 4331
Reputation: 5980
Managed to fix the problem eventually. It happened because I was using the SingleSong class inside a tabView. Due to this, I had to make a small, yet important adjustment.
private void doBindService() {
getApplicationContext().bindService( // getApplicationContext() fixed the problem here.
new Intent(getApplicationContext(), MusicService.class),
mConnection, Context.BIND_AUTO_CREATE);
}
Upvotes: 4