Original Title:
"Location set by GPS" still remains active after LocationServices.FusedLocationApi.removeLocationUpdates()
Edited Title:
GPS Location always remains active in notification tray after exit MapView
I want to disable remove the "Location set by GPS" and the location icon in top notification tray right after exit my map activity but it doesn't.
I'm using someone's code in stackoverflow which using following variable only:
GoogleMap mGoogleMap;
MapView mapView;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
MapLocationListener mapLocationListener;
What I've tried:
@Override
public void onPause() {
super.onPause();
//stop location updates when Activity is no longer active
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mapLocationListener);
if(mGoogleApiClient != null){
mGoogleApiClient = null;
}
if(mapView != null){
mapView = null;
}
if(mGoogleMap != null){
mGoogleMap = null;
}
if(mLocationRequest != null){
mLocationRequest = null;
}
if(mLastLocation != null){
mLastLocation = null;
}
if(mCurrLocationMarker != null){
mCurrLocationMarker = null;
}
if(mapLocationListener != null){
mapLocationListener = null;
}
}
activity.finish();
}
I understand that assigning null to my references should not be the solution because the location process is running in system background not using my app references.
And Yes, the location listener is no longer update but it just not disable the location icon on top like other apps did.
What I want:
disable the "Location set by GPS" and the location icon in top notification tray immediately after exit my map activity. I don't want to make my user thinks I'm doing "location hacking"
What I've found:
All the solution I found is using LocationManager.removeUpdate
which I don't have it. How can I declare it in my activity? Is this can really solve my problem?
EDIT: Attacthed Code:
Using Daniel Nugent's answer...
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.location.LocationManager.GPS_PROVIDER;
import static android.location.LocationManager.NETWORK_PROVIDER;
/**
* Created by elliotching on 11-Apr-17.
*/
public class ActivityMaps_ extends MyCustomActivity {
GoogleMap mGoogleMap;
MapView mapView;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
MapLocationListener mapLocationListener;
LocationManager locManager;
Button buttonSaveLocation;
double[] markerLocation;
private final static int mapZoomLevel = 18;
Context context = this;
AppCompatActivity activity = (AppCompatActivity) context;
private class GoogleApiClientConnection implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
@Override
public void onConnected(Bundle bundle) {
Log.d("onConnected", "onConnected.");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(50);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(context,
ACCESS_FINE_LOCATION)
== PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mapLocationListener);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
private class MapLocationListener implements com.google.android.gms.location.LocationListener {
@Override
public void onLocationChanged(Location location) {
// DO MY LOCATION UPDATE...
}
}
private class OnMapReady implements OnMapReadyCallback {
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d("onMapReady", "onMapReady.");
mGoogleMap = googleMap;
mGoogleMap.setOnMapClickListener(new OnMapTouched());
//if device OS SDK >= 23 (Marshmallow)
if (Build.VERSION.SDK_INT >= 23) {
//IF Location Permission already granted
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED) {
buildGoogleApiClient();
mGoogleMap.setMyLocationEnabled(true);
} else {
// Request Location Permission
checkLocationPermission();
}
}
//if device OS is version 5 Lollipop and below ( <= SDK_22 )
else {
if (checkLocationPermission_v22()) {
buildGoogleApiClient();
mGoogleMap.setMyLocationEnabled(true);
} else {
// AlertDialog TO EXIT MAP...
}
}
}
}
private class OnMapTouched implements GoogleMap.OnMapClickListener {
@Override
public void onMapClick(LatLng latLng) {
// CHANGE MY MARKER...
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// createMyView = setContentView(...)
createMyView(R.layout.activity_maps, R.id.toolbar);
mapLocationListener = new MapLocationListener();
buttonSaveLocation = (Button) findViewById(R.id.button_save_location);
buttonSaveLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveLocation();
}
});
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReady());
mapView.onResume();
}
private void saveLocation() {
Intent i = new Intent();
i.putExtra("savedlocation", markerLocation);
setResult(RESULT_OK, i);
this.onPause();
}
@Override
public void onPause() {
super.onPause();
if (mGoogleApiClient != null && mapLocationListener != null) {
Log.d("FusedLocationApi", "run!");
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mapLocationListener);
}
activity.finish();
}
protected synchronized void buildGoogleApiClient() {
Log.d("buildGoogleApiClient", "buildGoogleApiClient.");
GoogleApiClientConnection g = new GoogleApiClientConnection();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(g)
.addOnConnectionFailedListener(g)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
private boolean checkLocationPermission_v22() {
locManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
if (locManager.isProviderEnabled(GPS_PROVIDER) ||
locManager.isProviderEnabled(NETWORK_PROVIDER)) {
return true;
} else {
return false;
}
}
public static final int FR_PERMISSIONS_REQUEST_CODE_LOCATION = 99;
private void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) != PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{ACCESS_FINE_LOCATION}, FR_PERMISSIONS_REQUEST_CODE_LOCATION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case FR_PERMISSIONS_REQUEST_CODE_LOCATION: {
if (grantResults.length > 0 && grantResults[0] == PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
mGoogleMap.setMyLocationEnabled(true);
}
} else {
// AlertDialog to EXIT MAP...
}
return;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Build.VERSION.SDK_INT > 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
this.onPause();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
this.onPause();
return true;
}
return super.onOptionsItemSelected(item);
}
}