I am a beginner who made a simple program to show how service is work.
.....
toStartService = new Intent(this, SimpleService.class);
sc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MoreService.this, "SC: Binded", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(MoreService.this, "SC: Unbinded", Toast.LENGTH_SHORT).show();
}
};
startService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MoreService.this, "Starting Service", Toast.LENGTH_SHORT).show();
startService(toStartService);
}
});
stopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(toStartService);
}
});
bindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((isBound = bindService(toStartService, sc, BIND_AUTO_CREATE))) {
}
}
});
unbindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isBound) {
unbindService(sc);
isBound = false;
}
}
});
}
Why didnt the passing sc variables (on bindService()) doesnt call the sc.onServiceConnected() method? Whats wrong with the code?
I met this following condition:
When i press [startService] the service started well, then [stopService] the service stoped well.
When i press [startService] then the [bindService] does nothing, neither the [unbindService].
- When i press [bindService], its created the service, the [stopService] didnt work. I press [unbindService] the service is calling the onDestroy() method.
Why does the service that is created by bindService is destroyed when unbinded? I try to start the service with startService, but it cannot bind.
Arrgh help mee, sorry if i was wrong.