1

I have an android application with a foreground service that listens to gps location updates, I am using LocationManager with gps provider only.

I have this piece of code:

        Intent intent = new Intent(this, LocationUpdateReceiver.class);

        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, this.INTERVAL, 0.0f, pi);

I'm getting location updates through receiver. It is working correctly when screen is on, but when I turn the device screen off, it stop getting location updates. It is working on my redmi 5 device, and working as expected. but my problem apprears on huawei.

Note: this.INTERVAL = 10000; I'm using wakeLock to prevent the device from sleeping as follows:

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG:");
    wakeLock.aquire();

Also my service in Manifist.xml like this

          <service android:name="com.arttech.services.LocationService" android:enabled="true" />

And finally, I'm showing notification for my foreground service as follows:

    Notification.Builder ongoing = new Notification.Builder(getBaseContext()).setSmallIcon(R.drawable.logo).setContentTitle("Location Service").setOngoing(true);
    if (Build.VERSION.SDK_INT >= 26) {
        ongoing.setChannelId("myChannelID");

    }
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(NOTIFICATIONID, ongoing.build());     

Anyone can help me with this issue? I'm struggling for more than one month and I'm getting nowhere.

Thanks

Update:

I also tried to add my app as protected app manually through battery settings, but I get the same behavior. I tested some app called relive, and it records the GPS locations perfectly. What am I doing wrong?

UPDATE: Here are my OnStartCommand code:

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

    Log.d("omar","Service Started");
    LocationObserver.getInstance().addObserver(this);


    int SDK_INT = android.os.Build.VERSION.SDK_INT;
    if (SDK_INT > 8)
    {
        Log.d("omar_build",SDK_INT+"");

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);


    }

    CreateConnectionInstance(); // Connect to socket

    startTracking();
    isLocationServiceRunning = true;

    return START_STICKY;
}
Omar Taha
  • 97
  • 1
  • 11

2 Answers2

0

This is probably due to restriction applied by custom brands like Huawei and some others. You should add your app into whitelist of phone. Please check following link How to WhiteList app in Doze mode Android 6.0

UPDATE : make these two changes and then check

  1. Acquire wakelock into onStartCommand
  2. Acquire wakelock with some approximate time
  3. Release your wakelock when service destroy or when service work is done

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);

        if (powerManager != null)
            mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);

        if (mWakeLock != null)
            mWakeLock.acquire(TimeUnit.HOURS.toMillis(10));
Basit Ali
  • 51
  • 4
  • Thanks for your replay, I tried to add my app to protected app manually, but nothing changed, I installed an application called relive on that device, and it is working perfectly, I run the two apps and test both gps paths, the other application is totally perfect. – Omar Taha Dec 24 '18 at 14:34
  • Please provide your onStartCommand code here. so i can look into exactly what code you are using – Basit Ali Dec 24 '18 at 15:40
  • I will check your solution and feed you back – Omar Taha Dec 26 '18 at 10:58
  • I tried your code change, the problem still exists, sometimes it stops updating gps location while screen is off – Omar Taha Dec 31 '18 at 09:24
0

Maybe this helps you, from commonsware

https://commonsware.com/blog/2015/11/18/another-doze-edge-case-foreground.html

A foreground service with a partial WakeLock is supposed to block Doze mode, presumably with an eye towards music players and the like. However, Mr. Nalevka discovered that if the process with the foreground service also has the foreground activity, Doze mode takes over again.

Dianne Hackborn’s recommended workaround is to split the foreground service out into a separate process. Since there doesn’t seem to be a way to link to specific G+ comments, you’ll need to scroll way down the comments list on this post to get to their exchange.

blackkara
  • 4,900
  • 4
  • 28
  • 58