0

I am using location listener in my app. I have the code like:

    public void startlistning()
    {   
            locationListener = new LocationListener() {
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                Toast.makeText( getApplicationContext(),"Gps Enabled", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
                Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();
            }
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                location.getLatitude(); 
                location.getLongitude();
            }
        };
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

But don't know why the method onLocationChanged did not call when I restart device 1st time (without cellular data (no simcard) available). however, I am using best provider in my app. Please look into matter. Thanks

Chirag
  • 56,621
  • 29
  • 151
  • 198
Pankaj
  • 833
  • 12
  • 35
  • You might want to give some details around how you are invoking this method. – Franci Penov Mar 16 '11 at 09:05
  • I simply calling the method startlistning() on activity start. For calling getLastKnownLocation() , I have created a separate class MyLocationListner and then calling the getLastKnownLocation() as below in comment. Please help. Thanks – Pankaj Mar 16 '11 at 11:15

1 Answers1

2

Location listener method call when you change the location of device . Its not start when you restart device . If you want to invoke onLocationChanged method you have to change the location of device .


Please try this to get last known location.

    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    Criteria locationCritera = new Criteria();
    locationCritera.setAccuracy(Criteria.ACCURACY_FINE);
    locationCritera.setAltitudeRequired(false);
    locationCritera.setBearingRequired(false);
    locationCritera.setCostAllowed(true);
    locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

    String providerName = locationManager.getBestProvider(locationCritera, true);
    Location location  = locationManager.getLastKnownLocation(providerName);

    Log.i("--- Latitude",""+location.getLatitude());
    Log.i("--- Latitude",""+location.getLongitude());
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Hi, is there any way to open get last known location without starting google map? As I am unable to get location in my application when I restart device. – Pankaj Mar 16 '11 at 10:27
  • Hi, I add code for get last known location then stored into shared preference when every time location change, so when ever you restart device you can get it from shared preference. – Chirag Mar 16 '11 at 10:43
  • Actually I am doing the same things in my app, however, I am unable to get data from wi-fi network. It's working very fine for cellular data. The same code I am placing for your reference. Please see if something need to change or add. My code for last known location is as: – Pankaj Mar 16 '11 at 10:51
  • public Location getLastKnownLocation() { Location location=new Location(bestProvider); try { // Get the location manager... mLocMgr = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE); //Get best location provider by method.. Criteria criteria = new Criteria(); bestProvider = mLocMgr.getBestProvider(criteria, true); location = mLocMgr.getLastKnownLocation(bestProvider); } catch(Exception e) { Log.i("location exception", e.getMessage()); } return location; } – Pankaj Mar 16 '11 at 10:57