0

I want after open dialog and click on location button, detect user's location and show user's location and then user dismiss dialog, location listener removes and not get location from onLocationChanged, I try used locationManager.removeUpdate(locListener)on override dismiss but not any chance and location after dismiss again shows! why? How can i detect user's location after show dialog and after detect location, location listener remove? Thanks

EDIT:

public class DialogTest extends Dialog implements
android.view.View.OnClickListener {
Context context;
LocationManager locationManager;
LocationListener locationListener;

Location location;

public DialogTest(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.context = context;
    setContentView(R.layout.profile_dialog);

    getWindow().setBackgroundDrawableResource(R.drawable.background_dialog);

    firstAccessLocation=true;

    setCancelable(true);
    initialize();
}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    findAddress();
}

private void findAddress() {
    // if (gpsCheckBox.isChecked()) {
    locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    locationListener = new GPSLocationListener();
    locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0,
            0, locationListener);
    locationManager.requestLocationUpdates(
            locationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    // }
}

private void stopTracking(){
    if(locationManager!=null){
        locationManager.removeUpdates(locationListener);
    }
}

@Override
public void dismiss() {
    // TODO Auto-generated method stub
    stopTracking();
    gpsTurnOff();
    super.dismiss();
}

public class GPSLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub
        if(location!=null)
            Log.e("location", location.getLatitude()+"");
        location = loc;
        if (location != null&& firstAccessLocation) {
            setAddress(location.getLatitude(), location.getLongitude());
            firstAccessLocation=false;
        }
        Toast.makeText(context, "changed", Toast.LENGTH_LONG).show();
        Log.e("location", "changed");
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}

}

user3209380
  • 169
  • 1
  • 2
  • 14

1 Answers1

0

In new code you should use LocationClient rather then LocationManager. It's more efficient in getting the location with minimal energy(battery drain) with greater accuracy. Look here LocationClient vs LocationManager. With LocationClient established you can call getLastLocation() which provide you last location and doesn't start cyclic location updates. If you want to stick with LocationManager use requestSingleUpdate (String provider, LocationListener listener, Looper looper) method to get only one newest location from it.

Community
  • 1
  • 1
michal.z
  • 2,025
  • 1
  • 15
  • 10
  • I want get exact user location not last location. Last location some times is not correct location. My question is, i want to remove update detecting location but i don't know how can i do this. – user3209380 Aug 27 '14 at 09:50
  • So like I wrote in edit, use `requestSingleUpdate` method and you will not have to remove location updates. – michal.z Aug 27 '14 at 09:51
  • requestSingleUpdate is not available! I should use this in GPSLocationListener or in DialogTest class? – user3209380 Aug 27 '14 at 09:55
  • `public void requestSingleUpdate (String provider, LocationListener listener, Looper looper)` is a method of LocationManager so use it instead of `requestLocationUpdates` in your code. – michal.z Aug 27 '14 at 09:58
  • Thanks. And what is looper? and how can fill this? – user3209380 Aug 27 '14 at 10:04
  • `Looper` is a class used to run a message loop for a thread. You should pass your main thread looper here. To get it call `Looper.getMainLooper()`. – michal.z Aug 27 '14 at 10:06
  • requestSingleUpdate use in api level 9+ but i support api level 8+ :( Please say another way :) – user3209380 Aug 27 '14 at 10:13
  • Well first of all look at [Android Dashboards](https://developer.android.com/about/dashboards/index.html?utm_source=ausdroid.net). Devices with API below 10 is approximately 0.7%. Is it really worth it? :) However as an another solution I recommend using `LocationClient` and `LocationRequest` classes. They are a part of Google Play API. Besides last location use can also ask it for location updates with `requestLocationUpdates` method. – michal.z Aug 27 '14 at 10:22