Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 227
Registered: ‎12-16-2009
My Device: 9530,8900,9000
My Carrier: Airtel,Docomo

Two location Listener for Different Time Interval

Hi All,

Is it possible to implement Locationlistener for two diffrent LocationProvider?

 

I created two "BlackBerryLocationProvider" instance, and one LocationListenerImpl class which extends LocationListener class,

Now for both BlackBerryLocationProvider I am setting different LocationListenerImplobject, set Different Time Interval for both,

But in LocationUpdate Method of LocationListenerImpl class I got Last Assigned Provider,and updates at timeInterval assigned last.

 

So, Is it possible to set LocationListener with two different Interval? So I get LocationUpdate with Different,Different  Interval paralally?

Lathiya Himanshu
Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Two location Listener for Different Time Interval

no, afaik not.

API:
This method may, depending on the implementation, return the same LocationProvider instance as has been returned previously from this method to the calling application, if the same instance can be used to fulfil both defined criteria. Note that there can be only one LocationListener associated with a LocationProvider instance.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Trusted Contributor
Posts: 227
Registered: ‎12-16-2009
My Device: 9530,8900,9000
My Carrier: Airtel,Docomo

Re: Two location Listener for Different Time Interval

If I am using two Location Providerlike

 

BlackBerryLocationProvider provider1;

BlackBerryLocationProvider provider2;

 

private LocationListenerImpl locationListenerImpl1;
private LocationListenerImpl locationListenerImpl2;

 

on buttonClick

BlackBerryCriteria myCriteria =new BlackBerryCriteria();
                try
                {
                    provider1=(BlackBerryLocationProvider) BlackBerryLocationProvider.getInstance(myCriteria);
                    if ( provider1== null )
                    {
                        Runnable showUnsupportedDialog = new Runnable()
                        {
                            public void run()
                            {
                                Dialog.alert("Tracking:- Location service unsupported, exiting...");
                                //                            System.exit( 1 );
                            }
                        };
                        UiApplication.getUiApplication().invokeLater( showUnsupportedDialog ); 
                    }
                    else
                    {
                        locationListenerImpl1=new LocationListenerImpl();
                        provider1.setLocationListener(locationListenerImpl1,5, -1, -1);
                    }
                }
                catch (LocationException le)
                {
                    add(new RichTextField(" Failed to retrieve a location provider"));
                    System.err.println("Failed to retrieve a location provider");
                }

----------------------------------------------------

on Second ButtonClick

BlackBerryCriteria myCriteria =new BlackBerryCriteria();
                try
                {
                    provider2=(BlackBerryLocationProvider) BlackBerryLocationProvider.getInstance(myCriteria);

                    if ( provider2== null )
                    {
                        Runnable showUnsupportedDialog = new Runnable()
                        {
                            public void run()
                            {
                                Dialog.alert("Tracing:-  Location service unsupported, exiting...");
                                //                            System.exit( 1 );
                            }
                        };
                        UiApplication.getUiApplication().invokeLater( showUnsupportedDialog ); 
                    }
                    else
                    {
                        locationListenerImpl2=new LocationListenerImpl();
                        provider2.setLocationListener(locationListenerImpl2,10, -1, -1);
                    }
                }
                catch (LocationException le)
                {
                    add(new RichTextField(" Failed to retrieve a location provider"));
                }

------------------------ LocationListenerImpl.java class

 

private class LocationListenerImpl implements LocationListener
    {
        public LocationListenerImpl()
        {
            Location location=LocationProvider.getLastKnownLocation();
            if(location!=null)
            {
                if(location.isValid())
                {
                    Latitude=String.valueOf(location.getQualifiedCoordinates().getLatitude());
                    Longitude=String.valueOf(location.getQualifiedCoordinates().getLongitude());
                    DisplayLocation();
                    //add(new RichTextField("\n Last Known Location:- \nLatitude:- "+mLatitude+"\n Longitude:- "+mLongitude));
                }
            }
        }
        public void locationUpdated(LocationProvider provider, Location location)
        {
            try {
                if(location!=null && location.isValid())
                {
                    BlackBerryLocation bbLocation=(BlackBerryLocation)location;
                                       
                    double longitude = location.getQualifiedCoordinates().getLongitude();
                    double latitude = location.getQualifiedCoordinates().getLatitude();

                    Latitude=String.valueOf(latitude);
                    Longitude=String.valueOf(longitude);
                    DisplayLocation();
                    if(provider==provider1)
                    {
                        displayLocation1();                   
                    }
                    else if(provider==provider2)
                    {
                        displayLocation2();
                    }                
                    System.out.println("Lon" + longitude + " Lat "+ latitude);
               
                }
                else
                {
                    if(provider==provider1)
                    {
                        displayLocation1();                   
                    }
                    else if(provider==provider2)
                    {
                        displayLocation2();
                    }
                    synchronized (UiApplication.getEventLock())
                    {
                        add(new RichTextField("Location Invalid"));
                    }
                    //                txtLocation.setText("Location Invalid:-");
                    //rtfLocation.setText("Location Invalid:-"+"Lon" + mLongitude + " Lat "+ mLatitude);
                }
            }
            catch (Exception e)
            {
                error.setText("Location Update Error:- "+e.toString());
            }
        }
        public void providerStateChanged(LocationProvider provider, int newState)
        {

        }
    }

 

It always Returns the last provider1/provider2 which is registered last.

I want that Listener should update location with two time interval so, Am i doing wrong stuff?

Is there any Changes required in code? Is it possible?

 

 

Lathiya Himanshu
Developer
Posts: 149
Registered: ‎03-09-2009
My Device: Not Specified

Re: Two location Listener for Different Time Interval

BlackBerryLocationProvider.getInstance()

this line is important, it give a singleton, so with this method you will get always the same provider, look if you can get a provider with a different method.

 

But afaik they're not have a solution, but something possible si change que criteria of your provider after founding a position, that's work perfectly.

 

Hope it will help you

Pascal Bourque, Analyste-Programmeur : "je pense donc je programme"
Really want to thank a user? Click the Kudos! badge on right side of the message. When you do so, you are offering a thumbs up for good content and a pat on the back to its author.
Accepted Solutions is a way for you to choose the reply that best answers a thread that you've started. To mark a message as a solution: Click the Solution? icon on the reply.
Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Two location Listener for Different Time Interval

according to the API you will not always get the same provider, but the one matching the criteria best.
if a BB delivers a different provider for changed criteria or if the doc is more of a descriptive nature: no clue.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Two location Listener for Different Time Interval

Doesn't this answer the question?

 

Because the same criteria is being used, then the same Location Provider will be returned.  So in the code supplied by hims_3009, provider1 and provider2 will be the same.  This means the test,:

if(provider==provider1)

in locationUpdated method does not prove that the locationUpdated is the LocationListener for provider1.

 

So hims_3009,, I suspect you need to have two different Location Listener instances in order to distinguish this.  And the test you need is

if (this == locationListenerImpl1 ) { // listener 1 }

 

This makes sense to me, but am I just confused?

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Two location Listener for Different Time Interval

if the difference is only in the interval i'd suggest to use a single location provider that can service both requirements
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Two location Listener for Different Time Interval

Me too, but that was not the question. 

Trusted Contributor
Posts: 227
Registered: ‎12-16-2009
My Device: 9530,8900,9000
My Carrier: Airtel,Docomo

Re: Two location Listener for Different Time Interval

Hello Peter,

I also tried with Implementing single Provider and creating two instance of LocationListener1,LocationListener2

but in LocationUpdate method I always got the lastListener I registered..

And I think we will always get the LastListener we registered....

 

Simon,

f the difference is only in the interval i'd suggest to use a single location provider that can service both requirements ..

Yes,

I want two location at two different interval like, one at 5 second and another at 42 seconds.....

So I was thinking that If I can implement listener whcih will give me location at defined interval than I not required to check the condition in LocationUpdate method for Time, But Now I am thinking that I should use single listener and in update method check for TimeDifference.....

 

Thank you...

Lathiya Himanshu
New Developer
Posts: 9
Registered: ‎06-29-2011
My Device: Not specified
My Carrier: Not specified

Re: Two location Listener for Different Time Interval

Hi

 

I have had the same problem, and implemented the following solution that maybe will help you:

 

1.- Define a singleton class to listen the LocationListener (as usual). In my case GPS

2.- Define another NEW class that implement the LocationListener

3.- Register the NEW class as a listener for the GPS class

4.- When the GPS class receives location event, really does nothing with the event but resend it to the listeners of GPS.

 

That could be a quite garbled (a interface implementation to resend the events) but for me was the simple way to keep and order in the application.

 

Following is the code (accepts a single listener but you can adapt for multiple listeners)

 

 

public static void setListener(LocationListener ggl) {
        listener=ggl;
    }

 

public GPS() {
        boolean gps = false;
        try {
            BlackBerryCriteria criteria = new BlackBerryCriteria();
            criteria.setCostAllowed(false);
            criteria.enableGeolocationWithGPS();
            criteria.setSatelliteInfoRequired(true,false);
            lm = (BlackBerryLocationProvider)LocationProvider.getInstance(criteria);
            gps = lm.getState()!=LocationProvider.OUT_OF_SERVICE;   

            lm.setLocationListener(this, GPS_TIMEOUT, -1, -1);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

public void providerStateChanged(LocationProvider provider, int newState) {
        if(listener!=null) {
            try {
                listener.providerStateChanged(provider,newState);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void locationUpdated(LocationProvider provider, Location location) {
        if(listener!=null) {
            try {
                listener.locationUpdated(provider,location);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }

  }