0

I am struggling with persistent problem with Android app that has MQTT ServiceConnection running on background. When ever I switch to an other application that has fullscreen surface (game, youtube on fullscreen, etc) the application completely freezes when trying to bring it on front again. It even refuses to relaunch if closed from drawer. The app won't reopen unless completely killed. The weird thing is that if I run the application via debugger it works perfectly. It makes the problem difficult to get handle on. I have put on prints on beginning of each life cycle method, even before call to super class method. None of them get printed when the app hangs, which hints that it hangs some where on the system level. Now if I call stopService at onStop method, it works fine but defeats the purpose of having persistent service on the background. I have called unbindService also on onStop, but it does not help. With all the other application everything works fine, even you tube as long as it is not used in fullscreen. The dependency to the MQTT library is

compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'

and this is my onStop() method

@Override
protected void onStop() {
    FirebaseUserActions.getInstance().end(getIndexApiAction0());
    if(mBoundToService) {
        try {
            unbindService(serviceConnection);
            mBoundToService = false;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    super.onStop();
}

And the onStart() method would be this:

@Override
protected void onStart() {
    Log.d(TAG, "onStart has been called");
    super.onStart();
    if(!mBoundToService) {
        bindService(mqttIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
    FirebaseUserActions.getInstance().start(getIndexApiAction());
}

But this never gets called. From the logs I can see that MQTT service is receiving messages and produces no errors. Messages continue to be received even as the application is hanged so the background service is alive and running. The inlined Service connection class is like this:

private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder binder)
        {
            mqttService = ((MQTTService.MyBinder) binder).getService();
            mBoundToService = true;

            Log.d(TAG,"MQTT service has been bound");

            Handler handler = getHandler();
            //NOTIFY THE LOGIN ACTIVITY THAT THE DATA HAS BEEN ACQUIRED
            Message msg = handler.obtainMessage(Utils.HANDLE_MQTT_SERVICE_CONNECTED);
            Bundle bundle = new Bundle();
            bundle.putString(Utils.KEY_MQTT_SERVICE_BOUND,"Everything is complete");
            msg.setData(bundle);
            handler.sendMessage(msg);
        }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mBoundToService = false;
        Log.d(TAG, "MQTT service has been UNbound");
    }
};

The MQTT service is started like this from onCreate():

mqttIntent = new Intent(getApplicationContext(), MQTTService.class);
if(!mBoundToService){
    Log.d(TAG,"Starting MQTT Intent");
    startService(mqttIntent);
    Log.d(TAG,"Binding MQTT Service");
    bindService(mqttIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}

Am I missing something that I should be doing at the onStop() to be able to resume the application after having fullscreen application on front?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user2771538
  • 103
  • 1
  • 10

0 Answers0