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