0

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...

MatiRC
  • 189
  • 1
  • 3
  • 13
  • did you end up finding a solution? I'm having the exact same issues specifically on Samsung devices – Sameer J Feb 01 '19 at 07:00
  • 1
    @SameerJJ for external reasons this feature was removed so no. I couldn't manage to avoid the killing. Try asking in [Samsung Developers Forum](https://developer.samsung.com/forum/en?boardName=SDK&searchSubIdAll=&searchSubId=0000000047&topCtgy=01&searchType=ALL&searchText=). – MatiRC Feb 01 '19 at 16:08

1 Answers1

0

I've had some better but not perfect results using the new WorkerManager class in Android X, which leverages AlarmManager, JobScheduler or others internally depending on some undisclosed factors.

Another option to explore would be Firebase Job Dispatcher

Sameer J
  • 226
  • 3
  • 13
  • 1
    I forgot to update. I discovered (reading i don't know where) the Samsungs phones have an "Battery optimization" feature. This causes application kill if the app was not open in the last 3/4 days (depending on the configuration). – MatiRC Feb 12 '19 at 12:11