0

I'm kind of confused with bound services. I read Can anybody explain what is difference between unbound and bound service in android this post and based on that I'm trying out a sample. I created a Service and binding it to My activity as shown below

public class MyService extends Service {
private static final String TAG = "MyService";
MyBinder mMyBinder = new MyBinder();

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand: ");
    // start long running operation
    return super.onStartCommand(intent, flags, startId);
}

@Override
public boolean onUnbind(Intent intent) {
    Log.d(TAG, "onUnbind: ");
    return super.onUnbind(intent);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    Log.d(TAG, "onBind: ");
    return mMyBinder;
}

public class MyBinder extends Binder {
    public MyService getService(){
        return MyService.this;
    }
}
}

Activity is

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
MyService.MyBinder mMyBinder;
private Connect mConn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this, MyService.class);
    mConn = new Connect();
    bindService(intent, mConn, BIND_AUTO_CREATE);
}


public class Connect implements ServiceConnection {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.d(TAG, "onServiceConnected: ");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.d(TAG, "onServiceDisconnected: ");
    }
  }
 }

The service 'onStartCommand' is not called when I call bindService(intent, mConn, BIND_AUTO_CREATE);

should I call startService after binding? binding will not start the service?

If I already have a service which is started using startService and running , and I'm bindind the same service in some other activity, what happens if activity goes out of scope?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • is `onServiceConnected` called? if so, your activity is bound with your service and you can call its methods – pskink Nov 08 '17 at 16:11
  • it is called. but I'm doing some task in onStartCommand. so It won't be called while binding? –  Nov 08 '17 at 16:20
  • 1
    actually binding will start the service or not , if it has not been started already? –  Nov 08 '17 at 16:26
  • Just to make sure, have you declare the Service in your `AndroidManifest.xml`? – ישו אוהב אותך Nov 08 '17 at 16:31
  • it's declared in manifest and onServiceConnected also getting called –  Nov 08 '17 at 16:33
  • My question is `onStartCommand` will be called when a service is binded if the service not yet started? –  Nov 08 '17 at 16:35
  • so move it to some other method - `onStartCommand` is called only if you call `startService` – pskink Nov 08 '17 at 16:37
  • So in bounded service, `onBind` is the starting point of the service where I'm suppose to start the service operations if it is not yet started? –  Nov 08 '17 at 16:40
  • ok so it seems you should read [the basics](https://developer.android.com/guide/components/bound-services.html#Basics) first – pskink Nov 08 '17 at 17:06
  • I read the basics. And what I can understand is `onStartCommand` called when service is called by `startService` and `onBind` when called by `bindService`. Now in my acse, My service is some thing which can be called in both the ways. Inside my service I have a timer which will just start counting 1,2,3..etc till some limit in a seperate thread. –  Nov 09 '17 at 10:46
  • Since I can get call to my service call by both `bindService` and `startService` I can't assure which one will be called first. My timer should start only once and should not duplicate based on calls I get. So in this case should I start timer in `onCreate`? or should I keep a `isStarted` flag and start only if not yet started in `onBind` and `onStartCommand` calls? –  Nov 09 '17 at 10:46

0 Answers0