Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

java - make LocationServices stop at background

I developed an app that uses location as part of it.

Right now when a user signs into the app it asks for permission to use the location and after that, it samples the location every 30 minutes even if the user got out of the application.

Due to privacy matters as was suggested by lawyers, they recommended me to make the samples only when the users are active inside the application.

How can I stop getting location when the user is out of the app?

Currently, I use the following code:

private void initGPS() {
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient( this );

    locationRequest = LocationRequest.create();
    locationRequest.setPriority( LocationRequest.PRIORITY_HIGH_ACCURACY );
    locationRequest.setInterval( 30 * 60 * 1000 );
    locationRequest.setFastestInterval( 15 * 60 * 1000 );

    new GpsUtils( this ).turnGPSOn( isGPSEnable -> isGPS = isGPSEnable );

    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
                if (location != null) {
                    DO SOMETHING

                    if (!isContinue && mFusedLocationClient != null) {
                        mFusedLocationClient.removeLocationUpdates( locationCallback );
                    }
                }
            }
        }
    };

    if (!isGPS) {
        PopUps popUps = new PopUps();
        popUps.popSnack( getApplicationContext(), getWindow().getDecorView().findViewById( android.R.id.content ), getString( R.string.Location_Service ) );
    } else {
        isContinue = true;
        getLocation();
    }
}


private void getLocation() {
    if (ActivityCompat.checkSelfPermission( MyActivity.this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission( MyActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions( MyActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                1000 );

    } else {
        if (isContinue) {
            mFusedLocationClient.requestLocationUpdates( locationRequest, locationCallback, null );
        } else {
            mFusedLocationClient.getLastLocation().addOnSuccessListener( MyActivity.this, location -> {
                if (location != null) {
                    Lat_Coordinate = location.getLatitude();
                    Lon_Coordinate = location.getLongitude();
                } else {
                    mFusedLocationClient.requestLocationUpdates( locationRequest, locationCallback, null );
                }
            } );
        }
    }
}

@SuppressLint("MissingPermission")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult( requestCode, permissions, grantResults );
    if (requestCode == 1000) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            if (isContinue) {
                mFusedLocationClient.requestLocationUpdates( locationRequest, locationCallback, null );
            } else {
                mFusedLocationClient.getLastLocation().addOnSuccessListener( MyActivity.this, location -> {
                    if (location != null) {
                        Lat_Coordinate = location.getLatitude();
                        Lon_Coordinate = location.getLongitude();
                    } else {
                        mFusedLocationClient.requestLocationUpdates( locationRequest, locationCallback, null );
                    }
                } );
            }
        }
    }
}

My goal is to take a location sample when a user enters this activity and then every 30 minutes to take another. If the user signed out after 7 minutes, I won't take the next one.

From my understanding I should use that mFusedLocationClient.removeLocationUpdates( locationCallback );

But I'm not sure at what place to place it to make it work for the case the user stayed longer than 30 minutes.

Thank you

question from:https://stackoverflow.com/questions/65925490/make-locationservices-stop-at-background

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...