Wait for startService before bindService

I understand that if I want the service to start, even if nothing is limited to this, I must first start it using startService (Intent i).

My question is: WHAT IF I want to bind to a service right after it starts, will the following code guarantee that the service is created using startService ()?

Static method in a service class:

public static void actStart(Context ctx) {
    Intent i = new Intent(ctx, BGService.class);
    i.setAction(ACTION_START);
    ctx.startService(i);
}

Binding Activity:

BGService.actionStart(getApplicationContext());    
bindService(new Intent(this, BGService.class), serviceConnection, Context.BIND_AUTO_CREATE);
+5
source share
2 answers

I'm not sure what you are trying to do here, but "Context.BIND_AUTO_CREATE" creates the service and then contacts the service, even if it was not started.

, , onServiceConnected() serviceConnection:

 new ServiceConnection() { 
        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            //put your code here...
        } ...
0

Bugdayci, :

       ServiceConnection myConnection = new ServiceConnection() { 
            @Override
            public void onServiceConnected(ComponentName className,
                    IBinder service) {
                    ... your code that needs to execute on service connection       

            }


    @Override
    public void onServiceDisconnected(ComponentName name) {
        ... your code that needs to execute on service disconnection

    } 
        };

        Intent intent = new Intent(this, TheServiceClassName.class);
        bindService(intent, myConnection, Context.BIND_AUTO_CREATE);

...

bindService onServiceConnected() .

0

All Articles