2

I'm creating an application that needs to keep track of a lot of things the user is doing. This all needs to happen in the background and needs to be running continiously. To mention two: constant tracking of activity with google activity API and location tracking with google's geofence API.

I have created a (Intent)Service for each parameter I am tracking and everything is going well. Even when you kill the application, the service will start again on the background and perform as expected.

MY PROBLEM: As soon as I lock my phone, it stops tracking. As soon as I light up my screen or unlock it works again.

NOTE1: I'm using one main service which controls all other (intent)services sensing the parameters. (I'm not sure if this is the best way to do it or if I should create one big service?...).

What I've tried is the PARTIAL_WAKE_LOCK to keep awake my main service controlling all other services. This didn't work. I've also tried to use the PARTIAL_WAKE_LOCK to keep awake the services sensing the parameters. This did not work either.

NOTE2: I know this should not be done in real applications but it's a must. It's an application for an experiment and will never go in the playstore. Also all users will be notified what will be tracked and how this can effect their battery during the experiment.

 @Override
public void onCreate() {
    super.onCreate();
    ...
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "MyWakelockTag");

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (! keepCPUOn){
        wakeLock.acquire();
        keepCPUOn = true;

    }
}

Manifest:

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

Many thanks!

Wietse J.
  • 23
  • 1
  • 3
  • Have you tried [AlarmManager](https://developer.android.com/reference/android/app/AlarmManager.html). check [this](https://stackoverflow.com/questions/36399123/is-it-possible-to-run-service-in-android-platform-continuously-even-after-lockin/36814808#36814808) solution of JBA – Melchizedek Feb 23 '18 at 15:48
  • @Melchizedek, didn't find that solution. Will try it asap and post my findings! – Wietse J. Feb 23 '18 at 17:18
  • alright glad to help. – Melchizedek Feb 24 '18 at 03:09

3 Answers3

0

I am also creating app like your. location tracking work in background and UI screen doesn't lock. in which UI screen you are start location service please put below code in that java file.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_current_ride_new);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "MyWakelockTag");

    wakeLock.acquire();
}

This code is working perfectly for me.

Umair
  • 6,366
  • 15
  • 42
  • 50
  • Wouldn't this be very battery consuming if the phone can never put it's screen off? – Wietse J. Feb 24 '18 at 20:54
  • To avoid to enter sleep mode will always consume battery, and the solution proposed on this answer doesn't work with Android 6.0 or newer – from56 Feb 24 '18 at 21:21
  • Well another problem, I'm not using any UI at the time I'm tracking. The user can remove the App from the taskmanager and I've overwritten the onTaskRemove so that it restarts the app (only the services handling all the tracking and not the main UI screen). The main goal off the application is as less user input as possible (only a Start & Stop button). I could use the foreground service but then there's always the notification hanging there telling the user he's being tracked. Which may change his way of acting and thus is not suitable for the experiment. And thanks again for helping! – Wietse J. Feb 26 '18 at 14:32
0

Beginning with Android 6 and its new Doze mode the wake locks don't prevent the device to enter sleep mode, they are ignored

Proposed solution: Your service need to be a foreground service, that's it should call startForeground() and show a non dismissable notification, and you need to acquire a partial wake lock too.

from56
  • 3,976
  • 2
  • 13
  • 23
  • Yes, I have thought about a foreground service as well. This would indeed work perfectly. But it's sort of crusial that the user does not have the constant reminder it's being tracked. Since a foreground service requires a constant notification it isn't really what I need. I thank you for the suggestion! – Wietse J. Feb 24 '18 at 20:53
  • I worked a lot around this problem and as far as I know it's the only one solution. – from56 Feb 24 '18 at 21:18
0

I've just add my app in Protected apps in settings my phone Android 4.4 (this flug allow to keep running after the screen is turned off)

Grag2015
  • 591
  • 9
  • 15