while loop in main thread blocks listener updates?

by denzel » Thu, 05 Mar 2009 00:39:50 GMT


Sponsored Links
 Hi,

Take a look at the following code:

The first class is a locationmanager which holds the current and
previous location:

public class MyLocationManager implements LocationListener {
   private Location previousLocation;
   private Location currentLocation;

   private LocationManager locationManager;

   public MyLocationManager (Activity activity) {
      this.locationManager = (LocationManager)
activity.getSystemService(Context.LOCATION_SERVICE);
      this.locationManager.requestLocationUpdates
(LocationManager.GPS_PROVIDER, 0, 0, this);
   }

   @Override
   public void onLocationChanged(Location location) {
      this.lastLocation = location;
      Log.v("LocationManager", "Lat: " + location.getLatitude() + " -
Lon: " + location.getLongitude());
   }

   @Override
   public void onProviderDisabled(String provider) {}

   @Override
   public void onProviderEnabled(String provider) {}

   @Override
   public void onStatusChanged(String provider, int status, Bundle
extras) {}

}

Th second class is the main activity which acts like a server and uses
the previous location to do something:

public class Example extends Activity {
   private MyLocationManager myLocationManager;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      myLocationManager = new MyLocationManager(this);
      while (true) {
         // do something (like some server functionality)
         Location loc = myLocationManager.getPreviousLocation();
         // do something
      }
   }
}

The problem is that myLocationManager doesn't retreive any updates for
the location. I think it has to do with this while-loop that blocks
the main-thread  by continously executing. And because
mylocationmanager is also running in the main thread, he cant't
retrieve his location updates.

Does somebody know how to deal with this? I think I have to make use
of threads somewhere, but I don't know where exactly.

Thanks in advance!

kind regards,

Denzel
--~--~---------~--~----~------------~-------~--~----~



while loop in main thread blocks listener updates?

by denzel » Fri, 06 Mar 2009 03:55:30 GMT


 hanks for your response! My problem is solved!


On 4 mrt, 17:43, Romain Guy <romain...@google.com> wrote:
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. Accessing character data (CDATA) of XML element

Hi

I'm try to parse character data (CDATA) whit SAX Parser.

I support in this code for that:

http://www.java2s.com/Code/Java/XML/AccessingcharacterdataCDATAofXMLelement.htm

but when i use it in android (SDK 1.5) I get the next message:

W/System.err(  482): javax.xml.parsers.ParserConfigurationException:
No validating SAXParser implementation available

W/System.err(  482):    at
org.apache.harmony.xml.parsers.SAXParserFactoryImpl.newSAXParser
(SAXParserFactoryImpl.java:77)
W/System.err(  482):    at
com.whizzesforlife.UrlRequestHandler.getParsedData
(UrlRequestHandler.java:32)


And the in the UrlRequestHandler.java:32 i can read:

31       spf.setValidating(true);
32       SAXParser sp = spf.newSAXParser();

Can I include an special .jar library or something in order to add the
implementation required to to that?

Thanks in advance

-- 

2. How to check available RAM storage inside an application?

How to check available RAM storage inside an application?

-- 

3. Gathering id-android # 2

4. Special Thank's To Mr subanindiyo Ardi

5. Which one is more efficient, ImageView.setImageBitmap(bitmap) or ImageView.setImageDrawable(drawable)?

6. Translate Animation from Left To Right Infinite Times

7. New Year "2010" Google Give Free "2010" Gift Click & apply