F
F
Fotonick2016-02-09 22:32:04
Android
Fotonick, 2016-02-09 22:32:04

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) {
        }
    };

This method is just to replace text about GPS status
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));
    }

So when the GPS is disabled and the application is running, then when you turn on the GPS for the first time, everything is fine. But if you turn off GPS and turn it back on, then the application crashes when you call checkEnabled(), namely on the line
userLocalStore = new UserLocalStore(getActivity());
or if this method is commented out, then on the line
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
which, as I understand it, hints at a problem with the activity, after re-opening the GPS turn-on screen.
Apparently it is necessary to somehow check the readiness of the activity before doing something inside onProviderEnabled ? How?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Goryachkin, 2016-02-09
@Photonick

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 question

Ask a Question

731 491 924 answers to any question