开发者

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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜