Search code examples
androidandroid-gpsandroid-dozeandroid-doze-and-standby

Is it possible for my GPS application to continue running in the background while the tablet is on standby?


I have created an application that generates a tracklog of the Android devices location. A GPS coordinate is recorded at regular intervals and stored on the device for later download. Currently, when the phone goes on standby, the program stops recording points. Is there a method that would allow the application to continue documenting location while the unit is on standby? Thanks in advance.


Solution

  • I found two solutions. 1.) Use Wakelock

    public void wakeLock() {
             PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
             PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyApp::MyWakelockTag");
            wakeLock.acquire();
        }
    

    with the following added to the manifest XML file,

    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    

    or, 2.) use WindowManager to keep the device awake,

     public void noSleep() {
            if (bNoSleep == true){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            } else if (bNoSleep != true){
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
    }
    

    I've chosen the latter as a user selectable feature accessible via checkbox in the main workspace. I've also set this to engage automatically when a tracklog is initiated with the user having the option to disable this and allow standby/sleep to occur. I did implement a wakelock, but had some issues with it that may be related to a custom ROM on some of my Android devices. This is why I went ultimately went w/the windowmanager solution.