2

I am developing a small android application in which I want to find out the user's current location by using the network provider. I tried this in following ways but it's not giving me any output :

networklocationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener networklocationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        Log.i("********************************",
                "this is my network location " + location);
        String Location_text = "NETWORK LOCATION latitude:"
                + location.getLatitude() + " longitude:"
                + location.getLatitude();
        network_location.setText(Location_text);
    }

    public void onStatusChanged(String provider, int status,
            Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location
// updates
networklocationManager
        .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                networklocationListener);

I gave permissions in my manifest file like this

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

Is there any thing which I am missing ? Is this the correct way? Need help. Thank you...


public class MainActivity extends Activity implements LocationListener {

    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager gpslocationManager;
    private LocationManager networklocationManager;
    private LocationManager networklocationManager1;
    private String provider;
    private TextView gps_location;
    private TextView network_location;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gps_location = (TextView) findViewById(R.id.gps_location);
        network_location = (TextView) findViewById(R.id.network_location);
        networkLocation();
    }

    public void networkLocation() {
        networklocationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        LocationListener networklocationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
                Log.i("********************************",
                        "this is my network location " + location);
                String Location_text = "NETWORK LOCATION latitude:"
                        + location.getLatitude() + " longitude:"
                        + location.getLatitude();
                network_location.setText(Location_text);
            }

            public void onStatusChanged(String provider, int status,
                    Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };
        networklocationManager
                .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                        networklocationListener);
    }
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
nilkash
  • 7,408
  • 32
  • 99
  • 176
  • are you trying in emulator or in real device ? – Lucifer Apr 15 '13 at 11:13
  • Thank you lucifer for reply. I am trying it in real device. If I use provider as gps it gives me output. If I use network provider it's not giving output. – nilkash Apr 15 '13 at 11:17
  • ok, does your device has SIM ? – Lucifer Apr 15 '13 at 11:19
  • yes my device having SIM and also having proper range of network. – nilkash Apr 15 '13 at 11:20
  • can you upload more of your code ? – Lucifer Apr 15 '13 at 11:38
  • @lucifer see I update my question with my activity. – nilkash Apr 15 '13 at 11:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28245/discussion-between-lucifer-and-nilkash) – Lucifer Apr 15 '13 at 12:50
  • This is actually silly. I faced the same problem. My app wasn't updating the location even after I moved 12 kilometers from my previous location. However, Google Maps app is able to show my current location properly. After some searching around, people suggested I try restarting my phone. After the restart, my app started to get the location detail properly. Not sure if this is true only while installed during development/debug mode. If it is for install on every device, then it is surely a major turn off. – Sundeep Jul 18 '13 at 14:25
  • linking related questions http://stackoverflow.com/questions/13594932/network-provider-not-providing-updated-locations http://stackoverflow.com/questions/15747543/locationlistener-of-network-provider-is-enabled-but-onlocationchanged-is-never http://stackoverflow.com/questions/17734904/using-google-maps-location-without-activity http://stackoverflow.com/questions/16013601/network-location-provider-not-giving-location-android http://stackoverflow.com/questions/17169143/android-location-listener-in-service-does-not-work-until-i-reopen-wifi-mobile-ne – Richard Le Mesurier Feb 18 '14 at 17:11

3 Answers3

1

Make sure you enable Location Services in Settings! That should be the problem. It might be disabled (and this setting will usually be found in Location and Security in Settings)

Let me know if it works!

nithinreddy
  • 6,167
  • 4
  • 38
  • 44
  • OP is getting GPS in case of Network provider is GPS_PROVIDER, only problem is with NETWORK_PROVIDER – Lucifer Apr 15 '13 at 11:25
  • Yeah I am getting OP if I use gps provider instead of network provider but if I use network provider its not giving OP – nilkash Apr 15 '13 at 11:28
1

Is your network provider enabled?

boolean network_enabled;
try {
    network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
   ex.printStackTrace();
}
Rajeev
  • 1,374
  • 1
  • 11
  • 18
0

I hope this part of the code will help you extracted from vogella.

public class ShowLocationActivity extends Activity implements LocationListener {

    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);
        // Get the location manager
        locationManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
        // Initialize the location fields
        if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            onLocationChanged(location);
        } else {
            latituteField.setText("Location not available");
            longitudeField.setText("Location not available");
        }
    }

    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
    }
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Sreedev
  • 6,563
  • 5
  • 43
  • 66
  • Same problem with this also. If i use gps instead of network its gives output but if i use network not giving any output. What I am missing. – nilkash Apr 15 '13 at 11:40
  • You will only get the out put if the location is changed ...did you try moving and changing latitude and longitudes????Output will only be printed if it enters onlocationchanged..Did you tried changing your location?? – Sreedev Apr 15 '13 at 11:57
  • You also need to move quite a bit- network location is very inexact. Moving the phone from one room to another won't work. You need to move a significant distance, as if you were walking around a neighborhood. – Gabe Sechan Jul 13 '13 at 19:21