FusedLocationProviderClient перестает указывать местоположение в режиме ожидания - PullRequest
0 голосов
/ 21 октября 2018

Я работаю над приложением отслеживания местоположения, используя FusedLocationProviderClient.У меня есть фоновая служба, которая отслеживает местоположение телефона каждые 5 минут.

Все работает с ним хорошо, но как только телефон выходит из строя, а затем через 3-4 часа, фоновая служба перестает занимать место.Когда пользователь разблокирует телефон, отслеживание начнется снова.

Может кто-нибудь подсказать мне, что может быть причиной проблемы?

LocationUpdatesBroadcastReceiver

public class LocationUpdatesBroadcastReceiver extends BroadcastReceiver {
public static final String TAG = "LUBroadcastReceiver";

public static final String ACTION_PROCESS_UPDATES =
        "com.orangeapp.discountnotifier.receiver.action" +
                ".PROCESS_UPDATES";

@Override
public void onReceive(Context context, Intent intent) {
    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_PROCESS_UPDATES.equals(action)) {
            LocationResult result = LocationResult.extractResult(intent);
            if (result != null) {

                List<Location> locations = result.getLocations();
                Utils.setLocationUpdatesResult(context, locations);
//                    Utils.checKOffersByLatLng(context, locations);
                    Utils.sendNotification(context, Utils.getLocationResultTitle(context, locations));
                Log.e(TAG, Utils.getLocationUpdatesResult(context));
            }
        }
    }
}
}

MainActivity

private PendingIntent getPendingIntent() {

    // Note: for apps targeting API level 25 ("Nougat") or lower, either
    // PendingIntent.getService() or PendingIntent.getBroadcast() may be used when requesting
    // location updates. For apps targeting API level O, only
    // PendingIntent.getBroadcast() should be used. This is due to the limits placed on services
    // started in the background in "O".

    // TODO(developer): uncomment to use PendingIntent.getService().
//        Intent intent = new Intent(this, LocationUpdatesIntentService.class);
//        intent.setAction(LocationUpdatesIntentService.ACTION_PROCESS_UPDATES);
//        return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent intent = new Intent(this, LocationUpdatesBroadcastReceiver.class);
    intent.setAction(LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES);
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

protected void startLocationUpdates() {

    try {
        Log.i(TAG, "Starting location updates");
        Utils.setRequestingLocationUpdates(this, true);
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, getPendingIntent());
    } catch (Exception e) {
        Utils.setRequestingLocationUpdates(this, false);
        e.printStackTrace();
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...