java – Doesn't connect to GPS on some devices

Question:

Hello everyone. There is a geolocation application. It was checked on several devices, everything works, the location is determined quickly, on devices without a gps module it also finds the location by agps. The problem is that some users are constantly complaining that the application does not connect to gps, and the whole essence of the application is revealed only after the GPS connection is established. There are 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) {
        }      

Answer:

Apparently there is no connection due to the fact that the system did not give access to 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 chop down all the work right away.

If the system does not allow your application to access GPS, then you need to complete a request for access permissions.

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 shown a notification requesting permission to use the GPS for your application.

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

Scroll to Top
AllEscort