Abhilasha
Abhilasha

Reputation: 949

device location is always null

I have gone through many posts on SO regarding this issue: Tried everything in Here, Here and Here Nothing works. Everytime location is null. On device and on emulator :(. My GPS is on,internet is on and my manifest has following permissions:

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

I use following code to get the location i.e the longitude and latitude of the device...however in every case I get location as null

public void find_Location() {
        Log.d("Find Location", "in find_location");

        String location_context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getApplicationContext()
                .getSystemService(location_context);

        List<String> providers = locationManager.getProviders(true);
        for (String provider : providers) {
            locationManager.requestLocationUpdates(provider, 1000, 0,
                    new LocationListener() {
                        public void onLocationChanged(Location location) {}

                        public void onProviderDisabled(String provider) {
                        }

                        public void onProviderEnabled(String provider) {
                        }

                        public void onStatusChanged(String provider,
                                int status, Bundle extras) {
                        }
                    });
            Location location = locationManager.getLastKnownLocation(provider);

            if (location == null) {
                Toast.makeText(LbsGeocodingActivity.this, "Location Not found",
                        Toast.LENGTH_LONG).show();
            } else {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                Toast.makeText(LbsGeocodingActivity.this,
                        "LAT..." + latitude + "\nLONG..." + longitude,
                        Toast.LENGTH_LONG).show();
                // addr=ConvertPointToLocation(latitude,longitude);
                // String temp_c=SendToUrl(addr);

            }
        }
    }

Upvotes: 0

Views: 2307

Answers (2)

Abhilasha
Abhilasha

Reputation: 949

How could I be so stupid..My bad....In settings Use Wireless networks was not selected!....That cause all the problems !! Now I am able to get a location.

It took me 16 hrs to get to this...my productivity going down for sure! :(

Upvotes: 1

user1340450
user1340450

Reputation:

It takes some time before the position is determined, and it can change over time. It's probably just not available yet when you check it.

When a position fix becomes available, you will be notified in onLocationChanged.

Upvotes: 1

Related Questions