ServiceConnection.onServiceConnected() and startSe

2019-05-26 06:12发布

问题:

I have a very simple Activity:

public class MainActivity extends Activity
{
    private Intent      serviceIntent;
    public  MainService mainService;

    public ServiceConnection sc = new ServiceConnection()
    {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service)
        {
            mainService = ((MainService.MainServiceBinder)service).getService();
            Log.v("xxx", "[MainActivity]: onServiceConnected()");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0)
        {
            mainService = null;
            Log.v("xxx", "[MainActivity]: onServiceDisconnected()");
        }
    };

    @Override 
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        serviceIntent = new Intent(this, MainService.class);
    }

    @Override
    public void osStart()
    {
        super.onStart();
        // To call onServiceConnected() if the service already started
        bindService(serviceIntent, sc, BIND_DEBUG_UNBIND)
    }

    @Override
    public void onStop()
    {
        unbindService(sc);
        super.onStop();
    }

    // android:onClick procedure for Button in layout/main.xml
    public void doStartStopService(View Sender)
    {
        if(null == mainService)
        {
            startService(serviceIntent);
        }
        else
        {
            stopService(serviceIntent);
        }
    }
}

and a very simple Service:

public class MainService extends Service implements Runnable
{
    private boolean isInterrupted = false;
    private Thread  thread;

    class MainServiceBinder extends Binder
    {
        MainService getService()
        {
            return MainService.this;
        }
    }

    @Override
    public void onCreate()
    {
        Log.v("xxx", "[MainService]: onCreate()");      
        super.onCreate();
        thread = new Thread(this);
        thread.start();
    }

    // 1.6 only
    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
        Log.v("xxx", "[MainService]: onStart()");       
    }

    @Override
    public void onDestroy()
    {
        Log.v("xxx", "[MainService]: onDestroy()");     
        if(thread.isAlive())
        {
            isInterrupted = true;
        }               
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0)
    {
        Log.v("xxx", "[MainService]: onBind()");        
        return new MainServiceBinder();
    }

    @Override
    public boolean onUnbind(Intent intent)
    {
        Log.v("xxx", "[MainService]: onUnbind()");      
        return true;
    }

    @Override
    public void run()
    {
        Log.v("xxx", "[MainService]: run() started");

        while(!isInterrupted)
        {
            // ...
        }

        Log.v("xxx", "[MainService]: run() exiting");
    }

}

When I call doStartStopService() in a cycle, I got next logs:

[1] First startService():

02-10 07:31:49.775: V/xxx(16306): [MainService]: onCreate()
02-10 07:31:49.975: V/xxx(16306): [MainService]: onBind()
02-10 07:31:50.005: V/xxx(16306): [MainService]: onStart()
02-10 07:31:50.165: V/xxx(16306): [MainActivity]: onServiceConnected()
02-10 07:31:50.175: V/xxx(16306): [MainService]: run() started

[2] First stopService():

02-10 07:31:52.205: V/xxx(16306): [MainActivity]: onServiceDisconnected()
02-10 07:31:52.205: V/xxx(16306): [MainService]: onUnbind()
02-10 07:31:52.215: V/xxx(16306): [MainService]: onDestroy()
02-10 07:31:52.235: V/xxx(16306): [MainService]: run() exiting

[3] Second startService() - no onBind() and no onServiceConnected() calls:

02-10 07:31:54.355: V/xxx(16306): [MainService]: onCreate()
02-10 07:31:54.365: V/xxx(16306): [MainService]: onStart()
02-10 07:31:54.365: V/xxx(16306): [MainService]: run() started

Why when I call startService() second once, the functions onBind() and onServiceConnected() not called?

回答1:

if you bind to the service, the service onBind will call, so the second startService invoke, no bind to this service, you need invoke bindService(serviceIntent, sc, BIND_DEBUG_UNBIND) again.



回答2:

you need to just unbind your service in onServiceDisconnected(). Why would you want to bind your service just after unbinding it in your code??

Still, If you want the same just unbind your service in onServiceDisconnected() and notify your activity via an interface that you have just unbinded your service and once your activity is notified that you have unbinded your service let it decide whether it need to bind it or not.If it want to bind the same again, just let it do.