How to get Location if device screen is off?

281 Views Asked by At

I am working on app that has to get location after some fixed interval. It is working fine. But when device' screen is off, it is not getting location. For this I am trying WakeLock. When I use PowerManager.LOCATION_MODE_NO_CHANGE, it is crashing the app without any logs. Please help me to use this (PowerManager.LOCATION_MODE_NO_CHANGE). I am using it in onCreate of MainActivity.

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.LOCATION_MODE_NO_CHANGE, "My Tag");
        mWakeLock.acquire();
1

There are 1 best solutions below

1
Faizan Ahmad On BEST ANSWER

It was due to Power Saver mode. If power saver mode is on then device will not get location on screen off. So, to get location (on screen off), we have to turn off the screen saver mode.