×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    Multiple Listeners in Java ME/LBS service, how to?

    Hello,

    I am working on a GPS application. I want the application to react both when the location is updated as well as the status (e.g. when the availability of the LocationProvider is changed).

    I have a working solution for reacting on the updates of the location. I have created a class implementing Runnable and a PositionListener that returns the location to the class impementing the Runnable interface when updated. The Runnable is called from the midlet.

    Now I want to extend the functionality of my application to react on the status updates as well. My question is; What is the "pattern" for implementing a solution that should react on both location and status updates?

    Solution I can think of but haven't tried:

    1. Create a new runnable and positioning listener for status update.
    - But what how do I use LocationListener then? I cannot initiate two, can I?
    - Is it possible to have more than one instance of LocationProvider in one application? Or is it just references to the same I get when calling LocationProvider.getInstance()?

    2. Pass on a text field to the providerStateChange method in the listener. But the only thing I can do then is to update the textfield, not react on the change and to things. Hence, not a good solution for my purpose.

    Any advice would be helpful and most appreciated since I am new in this area.

    Thank you!

    Regards,
    Niklas

  2. #2
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    198

    Smile Re: Multiple Listeners in Java ME/LBS service, how to?

    Hi Niklas,

    I think it really depends on what you're trying to achieve.

    But an interesting comment you make is

    Quote Originally Posted by nicsoft View Post
    2. Pass on a text field to the providerStateChange method in the listener. But the only thing I can do then is to update the textfield, not react on the change and to things. Hence, not a good solution for my purpose.
    Which indicates a possible misunderstanding. Why do you think that you can't you react to the change? If the action you need to take is in your main application class, then create a public function in that class and call it from the location listener.

    Does the following help?

    Code:
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.location.*;
    import java.util.*;
    
    public class Appl extends MIDlet implements Runnable {
        private LocationHandler lh;
    
        public Appl() {
        }
    
        protected void startApp() {
            Display.getDisplay(this).setCurrent(new Form("Appl"));
            lh = new LocationHandler(this);
            new Thread(this).start();
        }
    
        public void run() {
            while (true) {
               try { Thread.sleep(100); } catch (Exception e) { }
            }
        }
    
        public synchronized void locationChanged(String message) {
            System.out.println(message);
        }
    
        protected void pauseApp() { }
        protected void destroyApp(boolean bool) { }
    }
    
    class LocationHandler implements LocationListener {
        private Appl appl;
    
        public LocationHandler(Appl appl) {
            this.appl = appl;
    
                try {
                    LocationProvider p = LocationProvider.getInstance(new Criteria());
                    p.setLocationListener(this, 1, -1, -1);
                } catch (Exception ex) { }
        }
    
        public void locationUpdated(LocationProvider p, Location l) {
            appl.locationChanged("new location");
        }
    
        public void providerStateChanged(LocationProvider p, int s) {
            appl.locationChanged("new provider state");
        }
    }
    - Mike
    NAVTEQ Network for Developers
    Developing Location Enabled Applications
    http://NN4D.com

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    Re: Multiple Listeners in Java ME/LBS service, how to?

    Hello Mike,

    Thank you for your response.

    That's one way I didn't think of. It will probably help me, I'll try it out! I'll post a comment here once I have tried it out in a couple of days.

    Thank you!

    Regards,
    Niklas

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    Re: Multiple Listeners in Java ME/LBS service, how to?

    Hello,

    It did work great. Thank you!

    Regards,
    Niklas

Similar Threads

  1. -- Java development for S80 with Eclipse --
    By Kalypso in forum Mobile Java Tools & SDKs
    Replies: 3
    Last Post: 2011-03-17, 10:35
  2. Multiple SMS listeners
    By nk_user in forum Mobile Java General
    Replies: 1
    Last Post: 2007-10-26, 22:43
  3. Replies: 4
    Last Post: 2006-09-05, 15:15
  4. Service Discovery
    By jimdeal in forum Bluetooth Technology
    Replies: 1
    Last Post: 2003-09-25, 09:41
  5. j2ME, java enabled, and the 3410
    By davidkangaroo in forum Mobile Java General
    Replies: 2
    Last Post: 2002-07-05, 22:30

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×