开发者

How to check if LocationManager.NETWORK_PROVIDER is available?

开发者 https://www.devze.com 2023-04-11 07:58 出处:网络
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开发者_开发问

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 ?


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


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


because here its always a little strange ;=)

i wrote some own piece of code to check for network provider availability:

put this in your onCreate() Method:

checkIfNetworkLocationAvailable(mContext_);

and this somewhere else in your code ;=)

/**
 * checks if NETWORK LOCATION PROVIDER is available
 */
private void checkIfNetworkLocationAvailable(Context context) {

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean networkLocationEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if(!networkLocationEnabled){
        //show dialog to allow user to enable location settings
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(mContext_.getString(R.string.txtTip));
        dialog.setMessage(R.string.msgLocationServicesDisabled);

        dialog.setPositiveButton(mContext_.getString(R.string.txtYes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
            }
        });

        dialog.setNegativeButton(mContext_.getString(R.string.txtNo), new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //nothing to do
            }
        });

        dialog.show();
    }

}

have fun, hope this helps someone :) was searching for exactly that piece of code :=)

cheers :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号