I made an app that mostly works as a tracker. The issue is that works correctly in almost every device i test for days and in background. Test in:
Motorola XT1563 , working since 2018-02-02
Motorola XT1635-02 , since 2018-01-24
Samsung E7, since 2018-01-19
This works perfectly, even if i reboot, lost connection, turn on/off location...
But in Samsung SM-G930F and Samsung SM-G950F works OK in background with acceptable frecuency for 3-4 Days but after that stops sending locations and never know nothing about the device at least the user opens the app again.
i know the new background location limits so i had to make some changes to my app. I follow this guide for that.
Location request:
private void createLocationRequest() {
mLocationRequest = new LocationRequest()
.setInterval(Constants.UPDATE_INTERVAL)
.setFastestInterval(Constants.UPDATE_FASTEST_INTERVAL)
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
private synchronized void buildGoogleApiClient(Context context) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();}
To start location updates
public void startLocationUpdates() {
if (isLocationPermissionGranted()){
Intent mRequestLocationUpdatesBroadcaster = new Intent(mContext, JobIntentBroadcaster.class);
mPendingIntent= PendingIntent.getBroadcast(mContext, 0, mRequestLocationUpdatesBroadcaster,
PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.getFusedLocationProviderClient(mContext).requestLocationUpdates(mLocationRequest, mPendingIntent);
The BroadcastReceiver:
public class JobIntentBroadcaster extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent.setClass(context, LocationIntentService.class);
LocationIntentService.enqueueWork(context, intent);
}
}
The JobIntentService(used before new android version):
public class LocationIntentService extends JobIntentService {
static final int JOB_ID = 1000;
public static void enqueueWork(Context context, Intent work) {
Log.d("enqueueWork","Se llama al jobintent service!");
enqueueWork(context, LocationIntentService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
if (LocationResult.hasResult(intent)) {
LocationResult locationResult = LocationResult.extractResult(intent);
Location location = locationResult.getLastLocation();
@SuppressLint("SimpleDateFormat") String mLastUpdateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
if (location != null) {
send to server
}
How can i handle this problem with new devices?
Is it necessary to make notifications (foreground service) to fix that?
Is this a consequence of new android versions or a Samsung battery safety thing?
UPDATE
I find out this problem is caused by Samsung Battery Optimization. This is the close solution i found but don't fit my requirements so the problem persist...