jM2.me
jM2.me

Reputation: 3969

Why is Android service being reset after unbind was called?

In my project android service is communication with serial device and it is being started when BOOT_COMPLETED broadcast is received. Occasionally I want to bind to the service for persistent connection. After unbind the service seems to restart, which is not very desirable when communication with serial device.

In Service:

@Override
public IBinder onBind(Intent intent)
{
  Log.d(TAG, "onBind");
  return messenger.getBinder();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
  ...
  return START_STICKY;
}

In Activity:

@Override
protected void onStart()
{
  super.onStart();
  bindService(new Intent(this, BMWiService.class), serviceConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop()
{
 super.onStop();
 if (serviceBound)
  {
    unbindService(serviceConnection);
    serviceBound = false;
  }
}

Upvotes: 0

Views: 617

Answers (1)

pskink
pskink

Reputation: 24720

start your service by calling startService() just before binding

Upvotes: 1

Related Questions