2 slaks SLaks 于 2013.03.12 17:27 提问

如何检查 LocationManager.NETWORK_PROVIDER 是否可用?

如何检查 LocationManager.NETWORK_PROVIDER 是否可用?我在 AndroidManifest.xml 里面配置了,但是需要在代码里检查一下使用 GPS_PROVIDER 是否可行。如何检查?请大家指点迷津。

2个回答

CuGBabyBeaR
CuGBabyBeaR   2013.03.12 22:26
已采纳
LocationManager lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
lm.isProviderEnabled(aLocationManager.GPS_PROVIDER);
lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Baby_Bonnie
Baby_Bonnie   2013.03.15 11:32

使用 Network provider 或者 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;
        }
Csdn user default icon
上传中...
上传图片
插入图片