locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)使用Wi-Fi始终返回false

时间:2015-12-16 08:38:58

标签: android

我试过

NETWORK_PROVIDER获取该地点的纬度和经度。

我的测试设备是LG Optimus G Pro(LG-F240L)Android 4.4.2,API 19。

使用Wi-Fi网络,而不是4G,3G

" isProviderEnabled(LocationManager.NETWORK_PROVIDER)"总是返回假。

任何人都可以帮助我吗?提前谢谢!

这是我的代码:

public class GetCurrentLocationActivity extends Service implements LocationListener {
  private final Context mContext;
  private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
  private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean isGetLocation = false;

Location location;
double latitude;
double longitude;

public GetCurrentLocationActivity(Context context){
    this.mContext = context;
    getLocation();
}



public Location getLocation(){
    try{
        locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        Log.d("test",""+isGPSEnabled);
        Log.d("test1",""+isNetworkEnabled);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(!isGPSEnabled && !isNetworkEnabled){
            showSettingsAlert();
        } else{
            this.isGetLocation = true;

            if(isNetworkEnabled){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);

                if(locationManager!=null){
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if(location!=null){
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            if(isGPSEnabled){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
                if(locationManager!=null){
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    if(location!=null){
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }else{
                showSettingsAlert();
            }
        }
    } catch (Exception e){
        e.printStackTrace();
    }
    return  location;
}

public void stopUsingGPS(){
    if(locationManager!=null){
        locationManager.removeUpdates(GetCurrentLocationActivity.this);
    }
}

public double getLatitude(){
    if(location!=null){
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude(){
    if(location!=null){
        longitude = location.getLongitude();
    }
    return longitude;
}

public boolean isGetLocation(){
    return this.isGetLocation;
}

public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    alertDialog.setTitle("GPS 사용유무셋팅");
    alertDialog.setMessage("GPS 셋팅이 되지 않았을수도 있습니다.\n 설정창으로 가시겠습니까?");

    // OK 를 누르게 되면 설정창으로 이동합니다.
    alertDialog.setPositiveButton("Settings",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(intent);

                }
            });
    // Cancle 하면 종료 합니다.
    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    alertDialog.show();
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}
     }

这是我的许可:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

1 个答案:

答案 0 :(得分:2)

代码似乎没问题。在权限方面,您只需要前两个:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

更换条件以检查您是否启用了GPS:

            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener)this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            } //end-fi isGPSEnabled