0

There is the following code for getting current location:

final LocationManager manager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener listener=new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {

        manager.removeUpdates(this);
        Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
        if (location!=null) {
                                doSomeAction();
        }
        else {
            Toast.makeText(MainActivity.this, LOCATION_IS_NULL_MESSAGE, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        manager.removeUpdates(this);
        String newProvider=provider.equals(LocationManager.GPS_PROVIDER) ? LocationManager.NETWORK_PROVIDER : null; 
        if (newProvider==null) {
            Toast.makeText(MainActivity.this, LOCATION_PROVIDERS_ARE_DISABLED_MESSAGE, Toast.LENGTH_LONG).show();
        }
        else {
            manager.requestLocationUpdates(newProvider, 0, 0, this);
        }
    }

    @Override
    public void onProviderEnabled(String provider) {}

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

};
String provider=manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ? LocationManager.GPS_PROVIDER : 
    LocationManager.NETWORK_PROVIDER;
Location location=manager.getLastKnownLocation(provider);
if (location!=null) {
                doSomeAction();
}
else {
    Toast.makeText(this, "1", Toast.LENGTH_LONG).show();
    manager.requestLocationUpdates(provider, 0, 0, listener);
}

I see toast "1" each time, but I've never seen "2" toast, therefore my location isn't be updated. Please, tell me, I need to get my current location - how can I fix my problem? I use network location provider.

Pratik Bhat
  • 7,166
  • 2
  • 35
  • 57
user1841247
  • 413
  • 1
  • 7
  • 20
  • Do you change your location actually? – Stan Jan 06 '13 at 11:35
  • I changes my location in 5 meters, but I believed that if there was no last known location, I would get my current location instantly, because I need to get current location only. – user1841247 Jan 06 '13 at 11:47
  • To make sure it work, try to simulate considerable [location change in emulator](http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator). – Stan Jan 06 '13 at 11:52
  • But my code is right yes? – user1841247 Jan 06 '13 at 11:55
  • I'm not sure about setting both `minTime` and `minDistance` to 0, Give them a reasonable non-zero values. – Stan Jan 06 '13 at 12:07
  • Are you able to check my code on your device? – user1841247 Jan 06 '13 at 12:13
  • Unfortunately, not right now. In addition to meaninful `minXXX` values, I suggest to comment out or add some logging into the `onProviderDisabled` handler. It may happen to be called and switch you to network provider which is apparently not capable of 5 meter distance change detection. – Stan Jan 06 '13 at 12:21
  • Please, tell me - my way to get currrent location is right? Because I just need to get current location – user1841247 Jan 06 '13 at 12:24
  • It looks similar to what I have in my app (and it works), but I already told you what the differences are. And I'm not sure it `must` fire update right after enabling the listener. It seems it can wait for a noticable location change. – Stan Jan 06 '13 at 12:28
  • 1
    try removing `manager.removeUpdates(this)`; in `onLocationChanged` – Pratik Bhat Jan 06 '13 at 12:46

2 Answers2

0

Did you actually attach the listener? With out attaching the listener its code is useless, but furthermore afaik: If the LocationManager has no Listener attached it will not update any location.

r-hold
  • 941
  • 8
  • 30
0

Try like this :

public class Locato extends Activity implements LocationListener {

LocationManager locman;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.idlayout);
locman = (LocatonManager) getSystemService(Context.LOCATION_SERVICE);

}

 public void onStatusChanged(String pr, int s, Bundle a) {}

 @Override
    public void onProviderDisabled(String provider) {
       Log.d("MyApp", "Provider disabled : " + provider);
    }

    @Override
    public void onProviderEnabled(String provider) {
Log.d("MyApp", "Provider enabled : " + provider);
}

 @Override
    public void onLocationChanged(Location location) {
// do something
    }

You must also add permissions to manifest : access coarse location and access fine location

TN888
  • 7,659
  • 9
  • 48
  • 84