The location class:
public class MyLocationListener implements LocationListener{
private Context cnt;
private ProgressDialog d;
public MyLocationListener(Context cnt){
this.cnt = cnt;
this.d.show(cnt, "", "Finding your location...");
}
@Override
public void onLocationChanged(Location loc){
loc.getLatitude();
loc.getLongitude();
this.d.dismiss();
String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
}/* End of Class MyLocationListener */
This is how I implement it:
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener(cnt);
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
I get a null pointer exception error when I add that progress dialog. When I take it out everything works fine.
Is the progress dialog conflicting with the Toast message?
EDIT:
It looks like onLocationChanged, well, changes often, so it ends up dismissing a dialog that was already dismissed.
Is there a way to only have the location found once?
with a boolean, but that won't be the best practice. in your case you would add an if before you dismiss your progressDialog:
if (this.d.isShowing())
this.d.dismiss();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.