Note that there are some explanatory texts on larger screens.

plurals
  1. POGPS location once
    text
    copied!<p>I have an app that finds the users location (loading in a progress dialog) using GPS or network signal , when it finds it's location, the coordinates are written in a text view . If the GPS doesn't find a location in 40 sec or the user clicks on the cancel button , then the dialog closes. The problem is that if I click to cancel the dialog , it doesn't dismiss. However, if I click again, it dismisses. Why do I have to click twice ?!? Below is the source code: </p> <pre><code>public class MainActivity extends Activity { private LocationControl locationControlTask; private boolean hasLocation = false; LocationHelper locHelper; protected Location currentLocation; private TextView myText; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myText = (TextView)findViewById(R.id.mytext); locHelper = new LocationHelper(); locHelper.getLocation(MainActivity.this, locationResult); locationControlTask = new LocationControl(); locationControlTask.execute(this); } protected void onStart() { super.onStart(); // .... new LocationControl().execute(this); } private class LocationControl extends AsyncTask&lt;Context, Void, Void&gt; { private final ProgressDialog dialog = new ProgressDialog(MainActivity.this); protected void onPreExecute() { this.dialog.setMessage("Tap to cancel"); this.dialog.setTitle("Searching"); this.dialog.setCancelable(true); this.dialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(which == Dialog.BUTTON_NEGATIVE) { dialog.dismiss(); Toast.makeText(MainActivity.this, "dialog canceled", Toast.LENGTH_SHORT).show(); } } }); this.dialog.show(); } protected Void doInBackground(Context... params) { //Wait 40 seconds to see if we can get a location from either network or GPS, otherwise stop Long t = Calendar.getInstance().getTimeInMillis(); while (!hasLocation &amp;&amp; Calendar.getInstance().getTimeInMillis() - t &lt; 40000) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }; return null; } protected void onPostExecute(final Void unused) { if(this.dialog.isShowing()) { this.dialog.dismiss(); } if (currentLocation != null) { //useLocation(); String text = "lat:"+currentLocation.getLatitude()+" long:"+currentLocation.getLongitude(); myText.setText(text); } else { Toast.makeText(MainActivity.this, "location could not be found", Toast.LENGTH_SHORT).show(); //Couldn't find location, do something like show an alert dialog } } } public LocationResult locationResult = new LocationResult() { @Override public void gotLocation(final Location location) { currentLocation = new Location(location); hasLocation = true; } }; @Override protected void onStop() { locHelper.stopLocationUpdates(); locationControlTask.cancel(true); super.onStop(); } } </code></pre> <p>The location helper: </p> <pre><code>public class LocationHelper { LocationManager locationManager; private LocationResult locationResult; boolean gpsEnabled = false; boolean networkEnabled = false; public boolean getLocation(Context context, LocationResult result) { locationResult = result; if(locationManager == null) { locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); } //exceptions thrown if provider not enabled try { gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) {} try { networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) {} //dont start listeners if no provider is enabled if(!gpsEnabled &amp;&amp; !networkEnabled) { return false; } if(gpsEnabled) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); } if(networkEnabled) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); } //GetLastLocation(); return true; } LocationListener locationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { locationResult.gotLocation(location); locationManager.removeUpdates(this); locationManager.removeUpdates(locationListenerNetwork); } public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extra) {} }; LocationListener locationListenerNetwork = new LocationListener() { public void onLocationChanged(Location location) { locationResult.gotLocation(location); locationManager.removeUpdates(this); locationManager.removeUpdates(locationListenerGps); } public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extra) {} }; private void GetLastLocation() { locationManager.removeUpdates(locationListenerGps); locationManager.removeUpdates(locationListenerNetwork); Location gpsLocation = null; Location networkLocation = null; if(gpsEnabled) { //if() gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); } if(networkEnabled) { networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } //if there are both values use the latest one if(gpsLocation != null &amp;&amp; networkLocation != null) { if(gpsLocation.getTime() &gt; networkLocation.getTime()) { locationResult.gotLocation(gpsLocation); } else { locationResult.gotLocation(networkLocation); } return; } if(gpsLocation != null) { locationResult.gotLocation(gpsLocation); return; } if(networkLocation != null) { locationResult.gotLocation(networkLocation); return; } //locationResult.gotLocation(null); } public void stopLocationUpdates() { locationManager.removeUpdates(locationListenerGps); locationManager.removeUpdates(locationListenerNetwork); } public static abstract class LocationResult { public abstract void gotLocation(Location location); } } </code></pre> <p>the xml :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;TextView android:id="@+id/mytext" android:textSize="15dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Main activity"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>and these 2 permissions in manifest </p> <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; </code></pre> <p>I would appreciate it if anyone would take a look at the bug.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload