tags:

views:

50

answers:

1

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.

A: 

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 by bindService(Intent, ServiceConnection, int): it requires the service to remain running until stopService(Intent) is called, regardless of whether any clients are connected to it. Note that calls to startService() are not nesting: no matter how many times you call startService(), a single call to stopService(Intent) will stop it.

Brian
thanks, "the service will only run as long as the calling context exists", then how do i bind to a service that is already running?
Keenan Gebze
If the service is started by `startService`, then you can bind to it and it will remain running after you unbind, but if it was started by `bindService`, then it will remain running only as long as the calling context exists.
Brian
Oh i just insert mBinder in the onBind method of the service, and then the ServiceConnection is working now! Why it can be like this?
Keenan Gebze