1

I am developing app to check which has to connect to MQTT Server to get messages from server.When android device enters in to DOZE mode,Client(Android) is automatically disconnecting because network is suspended but active.

Once device enters in to DOZE mode,I need to wake my device up,connect to the server which is using MQTT

I have written code for that,I need to make sure that whether i'm in correct track or not?

public void onReceive(Context context, Intent intent) {

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        boolean isIdle = pm.isDeviceIdleMode();
        Log.d(TAG, "  --- DozeModeReceiver isIdle--- "+isIdle);

        if (isIdle) {
            // the device is now in doze mode

            if (screenWakeLock == null) {
                    PowerManager mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                    screenWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyWakeLockTag");

                   Log.d(TAG, "  --- Going to Wake Screen ... --- " + result);

                    screenWakeLock.acquire();

                try {
                      /**
        *Code to  connect with MQTT Server
        */
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            if (screenWakeLock != null) {
                screenWakeLock.release();
                Log.i(TAG, "screenWakeLock Released ");
            }

        } else {
            // the device just woke up from doze mode
            Log.d(TAG, "  --- Woke up from Doze mode --- "+isIdle);
        }}
kavie
  • 2,154
  • 4
  • 28
  • 53
  • You probably need a high priority FBM/GCM. There is not really anyway to wakeup a device from doze locally or prevent doze other than by using a foreground service. However, the method setAndAllowWhileIdle in the AlarmManager may be of some use... – Elletlar May 02 '18 at 12:51
  • Also, have you tried starting a foreground service when the device goes idle? I'm not sure if that will work, but I'm curious. If you do try, it might be good to put it in another process because there was a defect in M where foreground services got dozed unless they were in a separate process.. – Elletlar May 02 '18 at 13:58
  • How using setAndAllowWhileIdle method will be helpful in my case..I didn't try to start foreground service.If it entries in to doze mode,I'm trying to connect with mqtt server – kavie May 03 '18 at 01:42
  • setAndAllowWhileIdle allows the app to operate during doze maintenance windows. But wakelocks alone cannot prevent the app from dozing in Android 6+. – Elletlar May 03 '18 at 07:48
  • So We cant avoid Doze mode in android wear.I mean If i make my app to be in ambient mode after interactive mode,From Ambient mode,Android device will definitely goes to DOZE mode If I keep android device as STATIONARY. – kavie May 04 '18 at 03:05
  • I didn't realise you were on wear. I've never tried to prevent doze on wear. All my wear apps are companion apps so I'm more worried about what the service behaviour is on the phone. – Elletlar May 04 '18 at 22:50
  • In my app I'm using MQTT to get message from third party server,if device enters into doze mode, Network got suspended.SO during doze mode. I'm not able to receive notification from MQTT server. **I can't go with fcm for some reason" – kavie May 07 '18 at 05:23

0 Answers0