如何在黑莓中使用GPS和AGPS连续获取位置

时间:2012-08-14 12:18:47

标签: blackberry

我已经编写了以下代码来查找黑莓中的位置,但有时候我会得到无效的位置实例。应该将什么值传递给位置监听器以连续获取位置?

package com.sims.sendlocation;

                /**
                 * 
                 * @author PramodKumarVishwakarma
                 *
                 */
                import java.util.Calendar;
                import java.util.TimerTask;

                import javax.microedition.location.Criteria;
                import javax.microedition.location.Location;
                import javax.microedition.location.LocationListener;
                import javax.microedition.location.LocationProvider;

                import com.sims.commonmethods.Constants;
                import com.sims.datahandler.GPRSHandler;
                import com.sims.rms.RMSHandler;

                public class LocationTimerTask extends TimerTask implements LocationListener {

                    private String locationStr;
                    private LocationProvider provider;
                    private String exception;

                    public void run() 
                    {
                        try
                        {

                            Criteria criteria = new Criteria();         
                            criteria.setCostAllowed(false);
                            //criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);       
                        provider = LocationProvider.getInstance(criteria);      
                        provider.setLocationListener(this, -1, -1, -1);
                    } 
                    catch (Exception e)
                    {
                        RMSHandler.addDataValues(Constants.CURRENT_LOCATION,"Provider Initialization: " + e.getMessage());
                        System.out.println("***********"+e);
                    }

                    if (checkTimeToSendMail())
                        startRequestThread();
                }

                public boolean cancel() {
                    return false;

                }

                private void startRequestThread()
                {
                    final GPRSHandler gprs = new GPRSHandler();


                    Thread thread = new Thread() {
                        public void run() {
                            try {
                                sleep(Constants.MINUTE * Constants.LOC_THREAD_WAIT);
                                {
                                    FindLocationReceiver location = new FindLocationReceiver();

                                    location.sendOrderToServer(RMSHandler.getDataValues(Constants.IP_INFO)+ gprs.updateConnectionSuffix().trim());
                                }
                            } catch (InterruptedException e) 
                            {
                                //Dialog.ask(Dialog.D_OK, "startRequestThread:   " + e);
                                }
                            }
                        };
                        thread.start();
                    }

                    private boolean checkTimeToSendMail() {
                        Calendar calendar = Calendar.getInstance();

                        int hour = calendar.get(Calendar.HOUR);

                        if (calendar.get(Calendar.AM_PM) == 0) {
                            if (hour >= Constants.MNG_TIME)
                                return true;
                        } else {
                            if (hour < Constants.EVNG_TIME)
                                return true;
                        }

                        return false;
                    }

                    public void locationUpdated(LocationProvider provider, Location location)
                {
                        if (location.isValid()) 
                        {
                            locationStr = location.getQualifiedCoordinates().getLatitude()+ "-" + location.getQualifiedCoordinates().getLongitude();

                        RMSHandler.addDataValues(Constants.CURRENT_LOCATION, locationStr);

                    } 
                    else

                        RMSHandler.addDataValues(Constants.CURRENT_LOCATION,"Not valid Location Provider");
                    }

                    public void providerStateChanged(LocationProvider provider, int newState) 
                    {

                    }
                }

有时我能够获得有效的位置,但有时我无法获得有效的位置。应该在Listener参数列表中传递什么值?

0 个答案:

没有答案