I am facing a memory leak problem during GPS location implementation. I have used basic implementation process of FusedLocationProvider to get last location.
But according to the backtrace, it is showing memory leak due to this implementation.
Is any one face same type of memory leak issue while implementing FusedLocationProvider API? And what should I follow to avoid memory leak here?
I am sharing my dumpstate log, memory trace log, and the code snippet for better understanding of the problem.
Here is the log link:
https://drive.google.com/drive/folders/1bzeACMj6erp2up9pgmueawixi8JVhICw?usp=sharing
Any kind of suggestion would be helpful.
Tested the issue on below device:
RAM: 2GB
Software Version: T295XXU4BUD3
Samsung
Here is the Code Snippet:
build.gradle
implementation 'com.google.android.gms:play-services-location:17.0.0'
Android Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Initialization
private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest locationRequest;
private LocationCallback locationCallback;
private boolean isGPS = false;
private boolean skipGPS;
private static final long UPDATE_INTERVAL = 5000, FASTEST_INTERVAL = 3000;
onCreate
skipGPS = false;
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
new GpsUtils(HouseholdActivity.this).turnGPSOn(new GpsUtils.onGpsListener() {
#Override
public void gpsStatus(boolean isGPSEnable) {
// turn on GPS
isGPS = isGPSEnable;
}
});
//prepareGpsAccess(); //causing memory leak..
}
Method Implementation:
public void getLocation() {
if (ActivityCompat.checkSelfPermission(HouseholdActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(HouseholdActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(HouseholdActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
AppConstants.LOCATION_REQUEST);
} else {
if (mFusedLocationClient != null) {
mFusedLocationClient.getLastLocation().addOnSuccessListener(HouseholdActivity.this, location -> {
if (location != null) {
Log.e(TAG, "Location: " + location.getLatitude());
Log.e(TAG, "Location: " + location.getLongitude());
} else {
Log.e(TAG, "Location: callback triggered...");
}
});
}
}
}
#SuppressLint("MissingPermission")
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.e(TAG, "Permission missing... result");
switch (requestCode) {
case 1000: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.getLastLocation().addOnSuccessListener(HouseholdActivity.this, location -> {
if (location != null) {
household.setLat(location.getLatitude());
household.setLon(location.getLongitude());
} else {
//mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
}
});
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
finish();
}
break;
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == AppConstants.GPS_REQUEST) {
isGPS = true; // flag maintain before get location
}
}
}
Google has recently resolved this memory leak issue in their latest play-services-location:20.0.0 release. Release Note.
So update your lib version to 20.0.0 and it will solve the problem, it did for me.
To know more about the history of this memory leak, check this and this.
Related
I have tried to follow a tutorial to the fullest. Even tried to make few changes. I admit I am very new to Android. Pardon the n00b and unprepared question.
This app crashes as soon as I open. I have tried to comment the updateGPS() call. Which made the app open but crashes again if I touch the "Location Updates" (sw_locationsupdates) switch which calls updateGPS().
`
tv_lat = findViewById(R.id.tv_lat);
tv_lon = findViewById(R.id.tv_lon);
tv_address = findViewById(R.id.tv_address);
tv_altitude = findViewById(R.id.tv_altitude);
tv_accuracy = findViewById(R.id.tv_accuracy);
tv_speed = findViewById(R.id.tv_speed);
tv_sensor = findViewById(R.id.tv_sensor);
tv_updates = findViewById(R.id.tv_updates);
tv_address = findViewById(R.id.tv_address);
sw_locationsupdates = findViewById(R.id.sw_locationsupdates);
sw_gps = findViewById(R.id.sw_gps);
locationRequest = new LocationRequest();
locationRequest.setInterval(1000 * DEFAULT_UPDATE_INTERVAL);
locationRequest.setFastestInterval(1000 * FAST_UPDATE_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationCallBack = new LocationCallback() {
#Override
public void onLocationResult(#NonNull LocationResult locationResult) {
super.onLocationResult(locationResult);
// save the location
updateUIValues(Objects.requireNonNull(locationResult.getLastLocation()));
}
};
sw_gps.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
if (sw_gps.isChecked()) {
// most accurate - use GPS
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
tv_sensor.setText("Using GPS sensors");
}
else {
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
tv_sensor.setText("Using Towers + WiFi");
}
}
});
sw_locationsupdates.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View w) {
if(sw_locationsupdates.isChecked()) {
// turn on lcation tracking
startLocationUpdates();
}
else {
// turn off tracking
stopLocationUpdates();
}
}
});
updateGPS(); // tried to comment this.
} //end of onCreate method
#SuppressLint("SetTextI18n")
private void startLocationUpdates() {
tv_updates.setText("Location is being tracked");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fsdLocationProviderClient.requestLocationUpdates(locationRequest, locationCallBack, null);
updateGPS();
}
}
#SuppressLint("SetTextI18n")
private void stopLocationUpdates() {
tv_updates.setText("Location is NOT being tracked");
tv_lat.setText("Not tracking location");
tv_lon.setText("Not tracking location");
tv_speed.setText("Not tracking location");
tv_address.setText("Not tracking location");
tv_accuracy.setText("Not tracking location");
tv_sensor.setText("Not tracking location");
fsdLocationProviderClient.removeLocationUpdates(locationCallBack);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_FINE_LOCATION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
updateGPS();
}
else {
Toast.makeText(this, "This app requires permission to be granted to function", Toast.LENGTH_SHORT).show();
finish();
}
break;
}
}
private void updateGPS () {
// get permission from user to track GPS
// get the current location from the fused API
// update the UI - i.e set all properties in their associated text view items
fsdLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// user provided the permission
fsdLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
// we got permission. Put the values of location, into the UI components.
updateUIValues(location);
}
});
} else {
// permission required
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_FINE_LOCATION);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
#SuppressLint("SetTextI18n")
private void updateUIValues(Location location) {
// update all of the text view objects with a new location
tv_lat.setText(String.valueOf(location.getLatitude()));
tv_lon.setText(String.valueOf(location.getLongitude()));
tv_accuracy.setText(String.valueOf(location.getAccuracy()));
if (location.hasAltitude()) {
tv_altitude.setText(String.valueOf(location.getAltitude()));
}
else {
tv_altitude.setText("Not available");
}
if (location.hasSpeed()) {
tv_speed.setText(String.valueOf(location.getAltitude()));
}
else {
tv_speed.setText("Not available");
}
Geocoder geocoder = new Geocoder(MainActivity.this);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
tv_address.setText(addresses.get(0).getAddressLine(0));
}
catch (Exception e) {
tv_address.setText("Unable to get street address");
}
}
}
`
I am following this youtube video.
Android Studio Tutorial - Build a GPS App
build.gradle has
implementation 'com.google.android.gms:play-services-location:21.0.0
AndroidManifest.xml has
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
Tried to use
locationRequest = new LocationRequest.Builder();
LocationRequest.Builder.setIntervalMillis(Priority.PRIORITY_PASSIVE);
LocationRequest.Builder.setMinUpdateIntervalMillis(Priority.PRIORITY_HIGH_ACCURACY);
LocationRequest.Builder.setPriority(Priority.PRIORITY_BALANCED_POWER_ACCURACY);
Didn't get very far.
Tried to use Try and catch updateGPS(). No luck
I am working on an android app that needs user location, every time user logins into the app.
I have written code to get user location and works fine, but the problem is if the user denies permission twice it didn't ask again, here is my code:
public class MainActivity extends AppCompatActivity {
String myLocation = "";
private LocationRequest locationRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(2000);
getCurrentLocation();
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (isGPSEnabled()) {
getCurrentLocation();
} else {
turnOnGPS();
}
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
getCurrentLocation();
}
}
}
private void getCurrentLocation() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (isGPSEnabled()) {
LocationServices.getFusedLocationProviderClient(MainActivity.this)
.requestLocationUpdates(locationRequest, new LocationCallback() {
#Override
public void onLocationResult(#NonNull LocationResult locationResult) {
super.onLocationResult(locationResult);
LocationServices.getFusedLocationProviderClient(MainActivity.this)
.removeLocationUpdates(this);
if (locationResult != null && locationResult.getLocations().size() > 0) {
int index = locationResult.getLocations().size() - 1;
double latitude = locationResult.getLocations().get(index).getLatitude();
double longitude = locationResult.getLocations().get(index).getLongitude();
myLocation = "Latitude: " + latitude + " Longitude: " + longitude;
Toast.makeText(getApplicationContext(), myLocation, Toast.LENGTH_LONG).show();
}
}
}, Looper.getMainLooper());
} else {
turnOnGPS();
}
} else {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
}
private void turnOnGPS() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getApplicationContext())
.checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
#Override
public void onComplete(#NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
Toast.makeText(MainActivity.this, "GPS is already tured on", Toast.LENGTH_SHORT).show();
} catch (ApiException e) {
switch (e.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvableApiException = (ResolvableApiException) e;
resolvableApiException.startResolutionForResult(MainActivity.this, 2);
} catch (IntentSender.SendIntentException ex) {
ex.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//Device does not have location
break;
}
}
}
});
}
private boolean isGPSEnabled() {
LocationManager locationManager = null;
boolean isEnabled = false;
if (locationManager == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
isEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
return isEnabled;
}
}
As, the user's location is mandatory for the working of app. Now, I want the app to:
1- ask for permission on activity start, if the permission is not granted or denied previously.
2- if permission is granted then proceed further else close the activity.
So, how can I achieve this.
Thanks
You can check whether the user has given permission or not using checkSelfPermission and if not then you ask for permission again else process with program flow.
if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
doStuff()
} else {
requestStoragePermission()
}
but the problem is if the user denies permission twice it didn't ask
again,
Yes, you cannot ask again once the user denies the permission with "Don't ask again" checked. This checkbox is normally checked by default on the second permission request. Beware that on api 11, there is no checkbox and the denial is automatic auto-denial auto-reset.
The way is that, you don't assume something about it, just put logic for handling it. If denied, you can only inform the user about the required permission. The official doc says: ref
If the ContextCompat.checkSelfPermission() method returns
PERMISSION_DENIED, call shouldShowRequestPermissionRationale(). If
this method returns true, show an educational UI to the user. In this
UI, describe why the feature, which the user wants to enable, needs a
particular permission.
info
I use a code that worked fine with Android 4.1. But this code doesn't work any more with Android 10. The code is as follows :
LocationManager Objgps = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Objlistener = new GPSlistener();
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
Objgps.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, Objlistener);
and :
private class GPSlistener implements LocationListener {
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onLocationChanged(Location location) {
Latitude.setText(CoordHrzSoleil.DDecToDMS(location.getLatitude()));
}
}
and within the manifest :
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
It seems that it misses some extra permissions, but I don't know what they are and how to apply them.
Thank you for your help.
Pierre.
Well, finally, I found the (or a) solution ; it's the same kind of gymnastics as for writing / reading files:
In the manifest you must :
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>"
Then, in the code :
if (Build.VERSION.SDK_INT >= 23) {
if (checkPermission()) {
} else {
requestPermission(); // Code for permission
}
}
...
private boolean checkPermission() {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
&& ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)) {
Toast.makeText(MainActivity.this, "Access fine and coarse location allows us to use GPS. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, GPS_PERMISSION_REQUEST);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, GPS_PERMISSION_REQUEST);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case GPS_PERMISSION_REQUEST:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("value", "Permission Granted, Now you can use GPS .");
} else {
Log.e("value", "Permission Denied, You cannot use GPS .");
}
break;
}
}
I haven't granted permission for "ACCESS_BACKGROUND_LOCATION". I'll have to check if it's necessary.
Cordially.
Pierre.
I am new to Android and in my current project I am trying to retrieve location using GPS. The code for obtaining the location is in a separate non-activity class, because the fragment where I display the coordinates is crammed.
I have tried to use the guidelines from developer.android.com but I can't get any permission prompt, and when I press the button in the fragment, I only get the default 0-values for latitude and longitude.
I have provided the permissions in the manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
The GPS locator class:
public class LocationProvider implements ActivityCompat.OnRequestPermissionsResultCallback {
private static final int PERMISSION_REQUEST_LOCATION = 1;
private Activity activity;
Location gps_loc;
Location final_loc;
double longitude;
double latitude;
private LocationManager locationManager;
public LocationProvider(Activity activity) {
this.activity =activity;
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_LOCATION) {
// Request for permission.
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
requestLocationPermission();
}
}
}
private void getLocation() {
// Check if the permission has been granted
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (gps_loc != null) {
Log.v("Loc_provider",gps_loc.toString());
final_loc = gps_loc;
latitude = final_loc.getLatitude();
longitude = final_loc.getLongitude();
}
} else {
requestLocationPermission();
}
}
private void requestLocationPermission() {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_LOCATION);
}
public String getCoordinates() {
return latitude + " "+ longitude;
}
}
The code in the fragment:
FloatingActionButton fab=view.findViewById(R.id.floatingActionButton);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
locProvider=new LocationProvider(requireActivity());
gpsCoordinates.setText(locProvider.getCoordinates());
}
});
Tips and solutions for fixing the code are most welcome.
UPDATE 1:
I moved the code in the fragment instead, so that it looks like below:
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!checkIfAlreadyhavePermission()) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
gpsCoordinates.setText(getCoordinates());
}
}
});
private String getCoordinates() {
if (gps_loc != null) {
Log.v("Loc_provider", gps_loc.toString());
final_loc = gps_loc;
latitude = gps_loc.getLatitude();
longitude = gps_loc.getLongitude();
}
return latitude + " " + longitude;
}
private boolean checkIfAlreadyhavePermission() {
int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
return result == PackageManager.PERMISSION_GRANTED;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Log.v("Grant_Results", String.valueOf(grantResults[0]));
Toast.makeText(getActivity(), "permission granted", Toast.LENGTH_LONG).show();
gpsCoordinates.setText(getCoordinates());
} else {
Toast.makeText(getActivity(), "permission denied", Toast.LENGTH_LONG).show();
}
break;
}
}
}
I now get the permission prompt, but the location still doesn't update (I get 0).
Please note:
Saving an Activity object is not recommended this may cause memory leaks. Activity may be destroyed and recreated (for example when screen orientation changes). Holding a reference to the old activity will cause you problems.
Implementing the ActivityCompat.OnRequestPermissionsResultCallback interface does nothing. Your method will never be called. You will have to call it explicitly from the activity.
You have not requested permission, so your permission prompt will never be shown and the default value of double that is 0 will get returned. You need to call your getLocation() method so that the control falls on the else block and your permission prompt is shown
I recommend you handle permission in the activity. Have the activity get the coordinates for the location. You can have the activity implement an interface, say LocationFetcher, with a method getCoordinates(). You can then call this in the fragment like so:
LocationFetcher locationFetcher = (LocationFetcher) activity;
gpsCoordinates.setText(locationFetcher.getCoordinates());
Fixed by implementing LocationListener in Fragment and then adding
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
in onRequestPermissionsResult().
I have an app which gets the users current location and then loads a Google Map and plots markers of interest in their area. It works flawlessly on everything below Marshmallow. I've added the run-time permissions check, and they are being set as I do get the prompt, and after I hit accept I see the Permission listed in the app details from the settings of the phone. However, I can not for the life of me figure out why I'm getting no location back.
I am using the example as seen here https://developer.android.com/training/location/retrieve-current.html
I have all the permissions set with the tag in the manifest. I even have my Fragment implementing the LocationListener. However, the onLocationChanged method never gets called. I am calling it within the onConnected method of the Google API Client like below..
#Override
public void onConnected(#Nullable Bundle bundle) {
try {
Log.d("MYAPPTAG","isConnected: " + mGoogleApiClient.isConnected());
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
} catch (SecurityException ex) {
myLocation = null;
}
}
the onConnected method DOES get called because I get my Log to the console. myLocation is always null though. I get a message in the console everytime I call getLastLocation that says
No Location to return for getLastLocation()
GoogleSignatureVerifier: com.myappname.android signature not valid. Found: LONG KEY HERE
Is there something special I need to do in Marshmallow?
my OnLocationChanged method
#Override
public void onLocationChanged(Location location) {
myLocation = location;
Log.d("MYAPPTAG", "LocatinChngListner, loc: " + location.getLatitude() + "," + location.getLongitude());
}
AndroidManifest.xml - permissions section (above node)
<!--- App permissions -->
<permission android:name="com.myappname.android.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.myappname.android.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.myappname.android.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.myappname.android.permission.C2D_MESSAGE"/>
onCreate Method snippet
createLocationRequest();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
createLocationRequest method
private void createLocationRequest(){
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(1000);
}
onStart() onStop() methods
#Override
public void onStart() {
if (!mGoogleApiClient.isConnected()) mGoogleApiClient.connect();
super.onStart();
}
#Override
public void onStop() {
if (mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect();
super.onStop();
}
I call this code below right after the Fragment onCreate method
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1 && (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_PERMISSION_USER_LOCATION);
} else {
googleMapsLocationPermissionContainer.setVisibility(View.GONE);
getUserLocationAndInitializeMap();
}
googleMapsLocationPermissionContainer is just a layout I overlay on the map until the permissions are granted.
getUserLocationAndInitializeMap()
try {
MapsInitializer.initialize(getActivity());
// Set reference for map object
if (map == null) {
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// Here I set the map to invisible then after I plot all the markers I set it to visible again
mapFragment.getView().setVisibility(View.INVISIBLE);
}
} catch (Exception e) {
// Show the google maps alert dialog
showGoogleMapsErrorDialog();
}
My approach for this is the following:
#Override
public void onResume() {
super.onResume();
initGpsTracker();
}
public synchronized void initGpsTracker() {
if (mMap != null) {
try {
checkIfPermissionAllowedForLocation();
} catch (SecurityException secex) {
Toast.makeText(getActivity(), "not enabled in manifest", Toast.LENGTH_SHORT).show();
}
}
}
/**
* rule is the following. First we check if the permissions are there. If not, we check if we can enable or not.
* If the permissions are check if gps is enabled.
*/
private void checkIfPermissionAllowedForLocation() {
//if permissions are set then we go to else, check for gps
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Request missing location permission.
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
askIfToRequestPermissions();
} else {
requestPermissions();
}
} else {
// Location permission has been granted, continue as usual.
if (!isGpsProviderEnabled()) {
askToEnableGPS();
} else {
mMap.setMyLocationEnabled(true);
}
}
}
private void askToEnableGPS() {
CustomFragmentDialog customFragmentDialog = CustomFragmentDialog.newInstance(getString(R.string.enable_gps_title),
getString(R.string.enable_gps_message),
getString(R.string.ok),
getString(R.string.cancel),
callback);
customFragmentDialog.show(getFragmentManager(), CUSTOM_TAG);
}
private void requestPermissions() {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_LOCATION);
}
private void enableGPS() {
if (enableLocationService != null) {
enableLocationService.askToEnableGps(locationCallback);
}
}
private GoogleAskToEnableLocationService.GpsCallback locationCallback = new GoogleAskToEnableLocationService.GpsCallback() {
#Override
public void onSuccess() {
initGpsTracker();
}
#Override
public void onResolutionRequired(Status status) {
try {
status.startResolutionForResult(getActivity(), CONNECTION_RESOLUTION_CODE);
} catch (IntentSender.SendIntentException setex) {
Toast.makeText(getActivity(), "Exception in sending intent:", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onError() {
Toast.makeText(getActivity(), "Location services unavailable!", Toast.LENGTH_SHORT).show();
}
};
private void askIfToRequestPermissions() {
CustomFragmentDialog customFragmentDialog = CustomFragmentDialog.newInstance(getString(R.string.enable_gps_title),
getString(R.string.enable_permissions_message),
getString(R.string.ok),
getString(R.string.cancel),
callback_permissions);
customFragmentDialog.show(getFragmentManager(), CUSTOM_TAG);
}
private CustomFragmentDialog.Callback callback = new CustomFragmentDialog.Callback() {
#Override
public void onPositiveButtonClicked(Bundle bundle) {
enableGPS();
}
#Override
public void onNegativeButtonClicked(Bundle bundle) {
}
};
private CustomFragmentDialog.Callback callback_permissions = new CustomFragmentDialog.Callback() {
#Override
public void onPositiveButtonClicked(Bundle bundle) {
requestPermissions();
}
#Override
public void onNegativeButtonClicked(Bundle bundle) {
}
};
private boolean isGpsProviderEnabled() {
return googleUtils.isGpsProviderEnabled();
}
//inside it is a normal check if locationManager can access
//the gps provider
if (locationManager.getProvider(provider) == null) {
return false;
}
Now, for request permission there are some callbacks:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CONNECTION_RESOLUTION_CODE:
switch (resultCode) {
case Activity.RESULT_OK:
initGpsTracker();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getContext(), "Gps not enabled:", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
break;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initGpsTracker();
} else {
Toast.makeText(getActivity(), "Manifest permission, not enabled", Toast.LENGTH_SHORT).show();
}
}
}
Now, finally the class where you deal with the request for the location so you do not have to go to settings:
public void askToEnableGps(final GpsCallback callback) {
if (locationClient == null) {
return;
}
mLocationRequestHighAccuracy = LocationRequest.create();
mLocationRequestHighAccuracy.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequestHighAccuracy.setInterval(30 * 1000);
mLocationRequestHighAccuracy.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequestHighAccuracy);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(locationClient, builder.build());
result.setResultCallback(getResultCallback(callback));
}
private ResultCallback<LocationSettingsResult> getResultCallback(final GpsCallback callback) {
return new ResultCallback<LocationSettingsResult>() {
#Override
public void onResult(LocationSettingsResult result) {
if (result != null) {
final Status status = result.getStatus();
if (callback != null) {
//final LocationSettingsStates statesResult = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
callback.onSuccess();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
callback.onResolutionRequired(status);
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
callback.onError();
break;
}
}
}
}
};
}
public interface GpsCallback {
/**
* callback method for when the result it is a success.
*/
void onSuccess();
/**
* callback for when user interaction it is required.
*
* #param status the result from the service needed for the resolution.
*/
void onResolutionRequired(Status status);
/**
* Callback for when the change it is not possible.
*/
void onError();
}