Search code examples
phpblackberrylocation

How to Send Location information periodically (Ex -15minutes) to php server from BlackBerry?


I am newbie to Blackberry development. I wanted to know the procedure for receiving & sending location data periodically to php server from a Blackberry application. How can I implement it? Any suggestions, link or code examples will be really helpful for me.


Solution

  • Your solution with code....

    public class GetLatLon {
    private LocationProvider provider,_locationProvider;
    public static Timer timer;
    double longitude=0.0,lattitude=0.0;
    Criteria criteria;
    public String number;
    public GetLatLon(final int duration)//Pass duration in constructor of this class as your periodically time interval
    
        timer = new Timer();
        this.duration=duration;
        timer.schedule(new TimerTask() {                    
            public void run() {
            startLocationUpdate(duration);
            }
        }, 0, duration*60*1000);   
    }
    
    public  class LocationListenerImpl implements LocationListener {
        public void locationUpdated(LocationProvider provider, final Location location) {
            System.out.println("---Location Updated-----");
            if (location.isValid()) {
                System.out.println("---Location Valid----");
                        longitude = location.getQualifiedCoordinates().getLongitude();
                        lattitude = location.getQualifiedCoordinates().getLatitude();
                        System.out.println("Lattitude :-=============================="+lattitude);
                        System.out.println("Longitude :-=============================="+longitude);
                                            //Here you will call your webservice to put data onto the php server.
                        _locationProvider.setLocationListener(null, 0, 0, 0);
                }else{
                System.out.println("else NOT valid--Cellsite valide=-----");
                setupCriteria();
                setupProvider();
            }
            System.out.println("---Location Not Valid----");
        }
        public void providerStateChanged(LocationProvider provider, int newState) {}
    }
    
    private void setupCriteria() {
        criteria = new Criteria();
        criteria.setCostAllowed(true);
        criteria.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
        criteria.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
        criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
    }
    
    
    private void setupProvider() {
        try {
            try {
                Thread.sleep(5000);
            } catch (Throwable e) {
               System.out.println(e.toString());
            }
            provider = LocationProvider.getInstance(criteria);
            provider.setLocationListener(
                    new LocationListenerImpl(), 1, 1, 1);
        } catch (Throwable t) {
            System.out.println(t.toString());
        }
    }
    public void startLocationUpdate() 
    {
        try{
            _locationProvider = LocationProvider.getInstance(null);
            if (_locationProvider != null) {
                _locationProvider.setLocationListener(
                        new LocationListenerImpl(), 1, 1, 1);
            } 
        }catch(LocationException le){
            System.out.println("----Exception Of Location--"+le);
        }
    }
    }