Why isn't onServiceConnected called?
I am a beginner who made a simple program to show how services 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, s开发者_JAVA百科c, BIND_AUTO_CREATE))) {
}
}
});
unbindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isBound) {
unbindService(sc);
isBound = false;
}
}
});
}
Why didn't passing the sc variable (on bindService()) 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.
This is the designed behavior of all of these methods. For example, in the bindService(Intent service, ServiceConnection conn, int flags)
method according to the documentation, the service will only run as long as the calling context exists:
The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.
For unbindService (ServiceConnection conn)
the documentation says:
Disconnect from an application service. You will no longer receive calls as the service is restarted, and the service is now allowed to stop at any time.
In the startService (Intent service)
documentation it says:
Using
startService()
overrides the default service lifetime that is managed bybindService(Intent, ServiceConnection, int)
: it requires the service to remain running untilstopService(Intent)
is called, regardless of whether any clients are connected to it. Note that calls tostartService()
are not nesting: no matter how many times you callstartService()
, a single call tostopService(Intent)
will stop it.
精彩评论