Answer the question
In order to leave comments, you need to log in
Why does the application crash if I turn on / off the GPS twice?
This is a licener for GPS, located in a fragment
public LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
showLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
checkEnabled();
}
@Override
public void onProviderEnabled(String provider) {
checkEnabled();
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#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 ActivityCompat#requestPermissions for more details.
return;
}
showLocation(locationManager.getLastKnownLocation(provider));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void checkEnabled() {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
userLocalStore = new UserLocalStore(getActivity());
user = userLocalStore.getLoggedInUser();
if (user.lastLocation != null) {
GPSview.setText(user.lastLocation);
} else {
GPSview.setText("Получение координат включено. Данные со спутника скоро появятся.");
}
} else {
GPSview.setText("Получение координат отключено");
}
//GPSview.setText("Enabled: " + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
}
Answer the question
In order to leave comments, you need to log in
In the fragment, you can catch the readiness of the activity in the onAttach event, this is where the activity is associated with the fragment and after that you can use getActivity without a twinge of conscience . Accordingly, you can either bind your lister after or in the onAttach event , or create a flag that will indicate the readiness of the activity and check its value when calling checkEnabled .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question