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);
}}