1

I tried

NETWORK_PROVIDER to get latitude and longitude of the place.

My test device is LG Optimus G Pro(LG-F240L) Android 4.4.2, API 19.

Using Wi-Fi network, not 4G, 3G

"isProviderEnabled(LocationManager.NETWORK_PROVIDER)" always return false.

Can anyone help me? Thank you in advance!

Here is my code :

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

}
     }

Here is my permission :

<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>
cherrypick
  • 91
  • 1
  • 10

1 Answers1

2

The code seems ok. In terms of permissions you need only the first two of them:

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

Replace the condition to check if you GPS is enabled like this:

            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
Octavian Ionel
  • 423
  • 6
  • 17