0

I'm stuck in a problem with a service binding that is giving me nuts.

I got an activity that is binding a service, and is frequent that the user go in and out of that activity.

The problem comes when the user goes out first time of the activity this one unBinds the service and when is going in again, do not binds again.

The activity calls the binding service this way:

@Override
protected void onStart() {
    super.onStart();
    Intent intent = new Intent(this, CService.class);
    intent.putExtra("id_local", (String) getIntent().getExtras().get("id_local"));
    intent.putExtra("id_send", (String) getIntent().getExtras().get("id_send"));

    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    registerReceiver(uiUpdated, new IntentFilter("SERVER_MESAGE"));
    mBound = true;
}

Where the mConnection is defined that way:

private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
                                   IBinder service) {
    CService.LocalBinder binder =(CService.LocalBinder) service;
        mService = binder.getService();
        Log.d("Service", "onServiceConnected");
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        Log.d("Service", "onServiceDisconnected");
        mBound = false;
    }
};

And in the onStop I unbindService:

@Override
protected void onStop() {
    if (mBound) {
        Log.d("ActivityStop", "Stoping activity");
        unregisterReceiver(uiUpdated);
        unbindService(mConnection);
        mBound = false;
    }
    super.onStop();
}

The onBind in the service is that one:

@Override
public IBinder onBind(Intent intent) {

    final String id_local = intent.getStringExtra("id_local");
    final String id_send = intent.getStringExtra("id_send");

    if (!misatgesList.isEmpty()) {
        misatgesList.clear();
    }

    mBackGroundTimer.schedule(new TimerTask() {
        @Override
        public void run() {

            String serverResult = restRecive(id_local, id_send,
                       misatgesList.size());
            if (serverResult != null) {
                misatgesList.addAll(procesMisatges(serverResult,
                             id_local));
                Intent i = new Intent("SERVER_MESAGE");
                i.putExtra("recive", serverResult);
                sendBroadcast(i);
            }
        }
    }, 0, 1000);


    return mBinder;
}

And the onUnBind is that one:

@Override
public boolean onUnbind(Intent intent) {
    mBackGroundTimer.cancel();
    misatgesList.clear();
    Log.d("ServiceOnUnBind", "ServiceOnUnBind");
    //stopSelf();
    return super.onUnbind(intent);
}

So my question would be, how I can bind again the service when the activity goes in again? Or what should I do to keep the binding alive until the user goes in the activity?

ALanao
  • 105
  • 10

2 Answers2

4

I found the solution!

What I meant was how to call again the onBind. That is done using the onRebind, that alows you to call again the onBind.

So, I created the onRebind:

    @Override
    public void onRebind(Intent intent) {
      super.onRebind(intent);
    }

Also, for the onRebind to work, you have to turn the return in the onUnbind to true.

 @Override
 public boolean onUnbind(Intent intent) {
   mBackGroundTimer.cancel();
   misatgesList.clear();
   Log.d("ServiceOnUnBind", "ServiceOnUnBind");
   //return super.onUnbind(intent);
   return true;
 }

For more explanation, check there: Bound Services

ALanao
  • 105
  • 10
0

Use getApplicationContext() API when binding to your service from Activity as below:

getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

getApplicationContext returns the global application context - the difference from other contexts is that for example, an activity context may be destroyed (or otherwise made unavailable) by Android when your activity ends. The Application context remains available all the while your Application object exists (which is not tied to a specific Activity)

AADProgramming
  • 6,077
  • 11
  • 38
  • 58
  • Thanks to the answer, i tried to add the `getApplicationContext()` to the code (in the `onStart()` of the activity and in the `onStop()` because there is were I do the `unbindService()`) and still dont' work... – ALanao Jun 06 '15 at 13:52