Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if LocationManager.NETWORK_PROVIDER is available?

How to check if LocationManager.NETWORK_PROVIDER is available?

I enabled in AndroidManifest.xml but I need to check that in code and if is not available to use GPS_PROVIDER. Can someone help me ?

like image 471
Damir Avatar asked Nov 27 '22 01:11

Damir


2 Answers

How to check if LocationManager.NETWORK_PROVIDER is available?

Use this Part to check whether Network Provider is Enabled or Not :

network_enabled=locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

This will return a boolean values :

true- if enabled.

false - not enabled.

additionally for checking if GPS Provider Enabled :

gps_enabled=locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

This will return a boolean values :

true- if enabled.

false - not enabled.

If you want the user to Enable Network or GPS Provider use this code to redirect user to Settings Page :

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
like image 78
Venky Avatar answered Dec 23 '22 13:12

Venky


here is the code for getting the location using Network provider and/or GPS provider

private Location getLocation() {            
        Location gpslocation = null;
        Location networkLocation = null;

        if(locMan==null){
          locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE);
        }
        try {
            if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);
                gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            }
            if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener);
                networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
            }
        } catch (IllegalArgumentException e) {
            //Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString());
            Log.e("error", e.toString());
        }
        if(gpslocation==null && networkLocation==null)
            return null;

        if(gpslocation!=null && networkLocation!=null){
            if(gpslocation.getTime() < networkLocation.getTime()){
                gpslocation = null;
                return networkLocation;
            }else{
                networkLocation = null;
                return gpslocation;
            }
        }
        if (gpslocation == null) {
            return networkLocation;
        }
        if (networkLocation == null) {
            return gpslocation;
        }
        return null;
    }

here it will check whether the GPS provider enable then get the location to gpsLocation if also getting the location using Network provider then check the time which was the fast that location object will return

if one of the provider is enable then that location object will be return

like image 27
Pratik Avatar answered Dec 23 '22 13:12

Pratik