Hello. There is an application with geolocation. It was checked on several devices, everything works, the location is determined quickly, on devices without a gps module it also finds a location by agps. The problem is that some users zealously complain that the application never connects to gps, and the whole essence of the application is revealed only after the GPS connection is established. Also, too many deletions of the application after installation, I think for the same reason. Below is the code responsible for the GPS connection, maybe I overlooked something in it? Tell me please.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } @Override protected void onResume() { super.onResume(); ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAltitudeRequired(false); criteria.setBearingRequired(true); criteria.setCostAllowed(false); String provider = mLocationManager.getBestProvider(criteria, true); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } mLocation = mLocationManager.getLastKnownLocation(provider); mLocationListener = new MyLocationListener(); mLocationManager.requestLocationUpdates(provider, 100, 0, mLocationListener); showCurrentLocation(mLocation); } public void showCurrentLocation(Location location) { if (location != null) { currentLat = location.getLatitude(); currentLon = location.getLongitude(); } } public class MyLocationListener implements LocationListener { public void onLocationChanged(Location location) { myLat = location.getLatitude(); myLon = location.getLongitude(); } public void onStatusChanged(String s, int i, Bundle b) { } public void onProviderDisabled(String s) { } public void onProviderEnabled(String s) { } 

    1 answer 1

    Apparently there is no connection due to the fact that the system did not give access to the GPS, judging by the code:

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } 

    You just immediately cut on the vine all the work.

    If the system denies access to your GPS application, then you need to fulfill the request for access permission.

    As an example, you can do it like this:

     public static final int REQUEST_LOCATION = 7 if (ActivityCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_LOCATION); } 

    If there are no rights, the user will be notified of a request for permission to use the GPS on your application.

    This solution is applicable for Android OS 6 и выше

    • By the way, I looked at the reviews, basically the problem is in people of Androidom above version 6. Trying to use your example. Is the LocationService highlighted in red? I can not understand why - ildar1989