当应用程序最小化时,FusedLocationProviderClient停止在后台服务中获取位置

时间:2019-05-04 18:18:40

标签: android location

希望所有的人都健康。如果您是一名Android开发人员,并且具有Google Map Location的使用经验,请阅读我的问题,如果您知道什么,将不胜感激。

问题:即使将应用程序最小化并且该服务仍在后台运行并获取位置更新,是否有任何方法可以在后台服务中获取位置更新。

  

PS:当应用程序为前台时,服务正在获取位置更新。   当应用程序最小化时,它突然停止获取位置更新。   发布前,我已经搜索并测试了几个答案   问题。

这是代码。代码中我缺少什么?

public class LocationService extends Service {

    public static final String LOCATION_SERVICE = "binarysole.c.distancetrackingapp.LocationService";
    Intent i = new Intent(LOCATION_SERVICE);
    private FusedLocationProviderClient mFusedLocationClient;
    private LocationCallback mLocationCallback;
    private LocationRequest mLocationRequest;

    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onCreate() {
        super.onCreate();
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        init();
        Log.e("LocationService","onStartCommand");
        return START_STICKY;
    }

    public void onDestroy() {
        removeLocationUpdate();
        Log.e("LocationService", "OnDestroy()");
         ActivityStackManager.getInstance().stopLocationService(this);
        super.onDestroy();
    }

    private void init() {
        this.mFusedLocationClient = LocationServices.getFusedLocationProviderClient((Context) this);
        this.mLocationCallback = new LocationCallback() {
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                LocationService.this.onNewLocation(locationResult.getLastLocation());
            }
        };
        setLocationInterval();
        getLastLocation();
        requestLocationUpdates();
    }

    private void getLastLocation() {
        try {
            if (ActivityCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION") == 0) {
                this.mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
                    public void onComplete(@NonNull Task<Location> task) {
                        if (!task.isSuccessful() || task.getResult() == null) {
                            Log.e("Location", " getLastLocation() Error");
                            return;
                        }
                        LocationService.this.onNewLocation((Location) task.getResult());
                       Log.e("Location", " getLastLocation() Success");
                    }
                });
            }
        } catch (SecurityException e) {
            StringBuilder sb = new StringBuilder();
            sb.append("Lost location permission.");
            sb.append(e);
            Log.e("Location", sb.toString());
        }
    }


    public void onNewLocation(Location location) {
        if (location != null) {
//            AppPreferences.saveLocation(new LatLng(location.getLatitude(), location.getLongitude()));
            i.putExtra("latitude",location.getLatitude());
            i.putExtra("longtitude",location.getLongitude());
            sendBroadcast(i);
            StringBuilder sb = new StringBuilder();
            sb.append(location.getLatitude());
            sb.append(",");
            sb.append(location.getLongitude());
            sb.append("  (");
            sb.append(Utils.getUTCDate(location.getTime()));
            sb.append(")");
            Log.e("Location", sb.toString());
            StringBuilder sb2 = new StringBuilder();
            sb2.append("accuracy = ");
            sb2.append(location.getAccuracy());
            Log.e("LocationService", sb2.toString());
            StringBuilder sb3 = new StringBuilder();
            sb3.append("timeDifference = ");
            sb3.append(System.currentTimeMillis() - location.getTime());
            Log.e("LocationService", sb3.toString());
            if (location.getAccuracy() <= 50.0f && System.currentTimeMillis() - location.getTime() <= 180000) {
               // ActivityStackManager.getInstance().stopLocationService(this);
                Log.e("LocationService", "stopLocationUpdates()");
            }
        }
    }


    public void setLocationInterval() {
        this.mLocationRequest = LocationRequest.create();
        this.mLocationRequest.setInterval((long) 1000);
        this.mLocationRequest.setFastestInterval((long) 1000);
        this.mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        Log.e("LocationInterval","setLocationInterval");
    }

    public void requestLocationUpdates() {
        if (ActivityCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION") == 0) {
            this.mFusedLocationClient.requestLocationUpdates(this.mLocationRequest, this.mLocationCallback, null);
            Log.e("LocationInterval","requestLocationUpdates");
        }
    }

    public void removeLocationUpdate() {
        try {
            this.mFusedLocationClient.removeLocationUpdates(this.mLocationCallback);
        } catch (Exception unused) {
        }
    }

}

0 个答案:

没有答案