Skip to content

Commit

Permalink
can't find location message was still posted even if location manager…
Browse files Browse the repository at this point in the history
… was turned off

git-svn-id: https://bostonbusmap.svn.sourceforge.net/svnroot/bostonbusmap@28 04fcf5f6-408b-428b-944a-e74021d52a6c
  • Loading branch information
ygfperson committed Jan 30, 2010
1 parent 84afb03 commit 9fa09e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
31 changes: 25 additions & 6 deletions bostonBusMap/src/boston/Bus/Map/Main.java
Expand Up @@ -119,6 +119,11 @@ public class Main extends MapActivity
*/
private final double timeoutInMillis = 10 * 60 * 1000; //10 minutes

/**
* What is used to figure out the current location
*/
private OneTimeLocationListener locationListener;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -246,7 +251,7 @@ public void run() {


@Override
protected void onDestroy() {
protected void onPause() {
if (mapView != null)
{

Expand All @@ -261,7 +266,12 @@ protected void onDestroy() {
editor.commit();
}

super.onDestroy();
if (locationListener != null)
{
locationListener.release();
}

super.onPause();
}


Expand Down Expand Up @@ -308,18 +318,27 @@ private void centerOnCurrentLocation() {
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
if (locationManager != null)
{
OneTimeLocationListener listener = new OneTimeLocationListener(mapView, locationManager, this);

String provider = locationManager.getBestProvider(criteria, true);
if (provider == null)
{
Toast.makeText(this, noProviders, Toast.LENGTH_LONG).show();
}
else
{
locationManager.requestLocationUpdates(provider, 0, 0, listener, getMainLooper());
if (locationListener != null)
{
locationManager.removeUpdates(locationListener);
}
else
{
locationListener = new OneTimeLocationListener(mapView, locationManager, this);
}

locationListener.start();

locationManager.requestLocationUpdates(provider, 0, 0, locationListener, getMainLooper());

Toast.makeText(this, "Finding current location...", Toast.LENGTH_SHORT).show();

//... it might take a few seconds.
//TODO: make sure that it eventually shows the error message if location is never found
}
Expand Down
18 changes: 17 additions & 1 deletion bostonBusMap/src/boston/Bus/Map/OneTimeLocationListener.java
Expand Up @@ -75,8 +75,14 @@ public OneTimeLocationListener(MapView mapView, LocationManager locationManager,
this.context = context;

terminateLocate = createTerminateLocate();
handler.postDelayed(terminateLocate, terminateAfter);
}

public void start()
{

handler.removeCallbacks(terminateLocate);
handler.postDelayed(terminateLocate, terminateAfter);
Toast.makeText(context, "Finding current location...", Toast.LENGTH_SHORT).show();
}

private Runnable createTerminateLocate()
Expand All @@ -88,11 +94,21 @@ private Runnable createTerminateLocate()
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "Cannot find location, try again later", Toast.LENGTH_LONG).show();

locationManager.removeUpdates(listener);
}
};
}

/**
* If the activity is pausing, silently remove any callbacks
*/
public void release()
{
handler.removeCallbacks(terminateLocate);
locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
Expand Down

0 comments on commit 9fa09e7

Please sign in to comment.