黑莓定位服务在真实设备上失败了?

时间:2011-10-31 14:21:46

标签: blackberry gps location

我一直在尝试使用Blackberry的GPS聆听器来获取经度和纬度值。我的设备是黑莓火炬。我使用的模拟器也是黑莓火炬。 GPS监听器似乎正在使用SIM卡,但一旦在真实设备上,它就会失败。当我说失败时,它不会获得经度和纬度值,相反,它甚至难以连接到GPS。我检查了我的选项菜单,并且我能够从位置设置中获取长值和纬度值,那么为什么我的应用程序无法执行此操作呢? 我将类handleGPS称为另一个类,即通过这样做:

new handleGPS();

正如我所说,使用SIM I,提供商在大约10秒后找到我的位置。在真实的设备上,我调试它,它确实达到了这个声明(因为打印了System.out)

try {
    lp = LocationProvider.getInstance(cr);
    System.out.println("location Provider");
    lp.setLocationListener(new handleGPSListener(), 10, -1, -1);
    //lp.setLocationListener(listener, interval, timeout, maxAge)
    System.out.println("location Provider after listener");
} catch (LocationException e) {
    e.printStackTrace();
}

但是没有返回任何值。以下是我的代码。

GPS等级:

public class handleGPS extends TimerTask {
 //Thread t = new Thread(new Runnable() {
private Timer timer;
LocationProvider lp = null;
public handleGPS()
{
    timer =new Timer();
    System.out.println("timer");
    GPS();

    //timer.schedule(this, 0, 10000);
    timer.schedule(this, 1000);
}

public void GPS() {
    Criteria cr = new Criteria();
    cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
    cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
    cr.setCostAllowed(false);
    cr.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
    //cr.setPreferredResponseTime(1000);

    System.out.println("GPS ()");
    try {
        lp = LocationProvider.getInstance(cr);
        System.out.println("location Provider");
        lp.setLocationListener(new handleGPSListener(), 10, -1, -1);
        //lp.setLocationListener(listener, interval, timeout, maxAge)
        System.out.println("location Provider after listener");
    } catch (LocationException e) {
        e.printStackTrace();
    }
}
// });

public void run() {
    // TODO Auto-generated method stub
    lp.setLocationListener(new handleGPSListener(), 10, -1, -1);
}

}

这是处理程序:

public class handleGPSListener implements LocationListener {
Coordinates c = null;
private static double lat=0.00;
private static double lon=0.00;



Database sqliteDB;
String username;
public static final String NAMESPACE = "http://tempuri.org/";
public String URL = "http://77.245.77.195:60010/Webservice/IDLMobile.asmx?WSDL";
public static final String SOAP_ACTION = "http://tempuri.org/Get_OfferCount_By_Location";
public static final String METHOD_NAME = "Get_OfferCount_By_Location";
private double x,y;
public void locationUpdated(LocationProvider loc, Location location) {  //method to update as the location changes.
    System.out.println("class handle GPS Listener");
    if (loc == null) {  //condition to check if the location information is null.
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert("GPS not supported!"); //dialog box to alert gps is not started.
                System.out.println("Problem 1");
                return;
            }
        });
    } else {    //if not checked.
        System.out.println("OK");
        switch (loc.getState()) {   //condition to check state of the location.
        case (LocationProvider.AVAILABLE):  //condition to check if the location is available.

            System.out.println("Provider is AVAILABLE");

            try {
                location = loc.getLocation(-1); //location to get according to user present.
            } catch (LocationException e) {
                return;

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (location != null && location.isValid()) {   //condition to check if the location is not null and is valid.
                c = location.getQualifiedCoordinates(); //to get the coordinates of the location.
            }
            if (c != null) {    //condition to check if the location is not null.
                lat = c.getLatitude();  //retrieve the latitude values into variable.
                lon = c.getLongitude(); //retrieve the longitude values into variable.

                System.out.println("lat and lon"+lat+lon);
                UiApplication.getUiApplication().invokeLater(
                        new Runnable() {
                            public void run() {
                                updateFields();
                                getValues();
                                // Dialog.alert(lat+"GPS supported!"+lon);
                                return;
                            }

                            private void getValues() {
                                // TODO Auto-generated method stub
                                try {
                                    URI uri = URI
                                            .create("file:///SDCard/"
                                                    + "database3.db");  //database3 to retrieve the values from location table.
                                    sqliteDB = DatabaseFactory.open(uri);
                                    Statement st = null;
                                    st = sqliteDB
                                            .createStatement("SELECT Latitude,Longitude FROM Location");//statement to retrieve the lat and lon values.
                                    st.prepare();
                                    Cursor c = st.getCursor();//cursor to point.
                                    Row r;
                                    int i = 0;
                                    while (c.next()) {  //loop to execute until there are no values in the cursor.
                                        r = c.getRow(); //store the values in row.
                                        i++;
                                        lat=Double.parseDouble(r.getString(0)); //retrieve the latitude values from the database and store in variable.
                                        lon=Double.parseDouble(r.getString(1)); //retrieve the longitude values from the database and store in variable.
                                        System.out.println(r.getString(0)
                                                + " Latitude");
                                        System.out.println(r.getString(1)
                                                + " Longitude");

                                    }

                                    st.close();
                                    sqliteDB.close();
                                }

                                catch (Exception e) {
                                    System.out.println(e.getMessage()
                                            + " wut");
                                    e.printStackTrace();
                                }
                                try {
                                    URI uri = URI
                                            .create("file:///SDCard/"
                                                    + "database1.db");
                                    sqliteDB = DatabaseFactory.open(uri);
                                    Statement st = null;
                                    st = sqliteDB
                                            .createStatement("SELECT Name FROM People");
                                    st.prepare();
                                    Cursor c = st.getCursor();
                                    Row r;
                                    int i = 0;
                                    while (c.next()) {
                                        r = c.getRow();
                                        i++;
                                        username=r.getString(0);

                                        System.out.println(r.getString(0)
                                                + "Name");


                                    }

                                    st.close();
                                    sqliteDB.close();
                                }
                                catch(Exception e)
                                {
                                    e.printStackTrace();
                                }
                                SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
                                rpc.addProperty("Username", username);
                                rpc.addProperty("latitude", String.valueOf(lat));
                                rpc.addProperty("longitude", String.valueOf(lon));
                                rpc.addProperty("distance", "1.5");
                                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                        SoapEnvelope.VER11);

                                envelope.bodyOut = rpc;
                                envelope.dotNet = true;
                                envelope.encodingStyle = SoapSerializationEnvelope.XSD;

                                HttpTransport ht = new HttpTransport(URL);
                                ht.debug = true;

                                try {
                                    ht.call(SOAP_ACTION, envelope);
                                    System.out.println("IN TRY");
                                    SoapObject resultProperties = (SoapObject) envelope
                                            .getResponse();

                                    System.out.println("username INT RIGHT HERE " + resultProperties.getProperty(0));
                                    System.out.println("username INT RIGHT HERE " + resultProperties.getProperty(1).toString());
                                    System.out.println("username INT RIGHT HERE " + resultProperties.getProperty(2).toString());
                                    System.out.println("lat and lon PARSE HERE " + lat+"\n"+lon);

                                    /* here is the notification code */

                                    //ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();    
                                    //EncodedImage image = EncodedImage.getEncodedImageResource("logosmall.png");
                                    //ApplicationIcon icon = new ApplicationIcon( image );

                                    //ApplicationIndicator indicator = reg.register( icon, false, true);
                                    //indicator.setIcon(icon);
                                    //indicator.setVisible(true);

                                    //setupIndicator();
                                    //setVisible(true, 0);

                                    //NotificationsManager.triggerImmediateEvent(1, 0, 20, null);
                                    //NotificationsManager.




                                    /* end notification code */


                                } catch (org.xmlpull.v1.XmlPullParserException ex2) {

                                } catch (Exception ex) {

                                    String bah = ex.toString();

                                }
                            }

                            private void updateFields() {
                                // TODO Auto-generated method stub
                                try {

                                    URI myURI = URI
                                            .create("file:///SDCard/"
                                                    + "database3.db");
                                    sqliteDB = DatabaseFactory.open(myURI);
                                    Statement st = null;
                                    Statement oops = null;
                                    st = sqliteDB
                                    .createStatement("SELECT Latitude,Longitude FROM Location");
                            st.prepare();
                            Cursor c = st.getCursor();
                            Row r;
                            int i = 0;
                            while (c.next()) {
                                r = c.getRow();
                                i++;
                                x=Double.parseDouble(r.getString(0));
                                y=Double.parseDouble(r.getString(1));
                                System.out.println(r.getString(0)
                                        + " Latitude in update fields");
                                System.out.println(r.getString(1)
                                        + " Longitude in update fields");

                            }
                                    st = sqliteDB
                                            .createStatement("UPDATE Location SET Latitude='"
                                                    + lat
                                                    + "' "
                                                    + "WHERE Latitude="
                                                    + "'" + x + "'" + "");
                                    oops = sqliteDB
                                            .createStatement("UPDATE Location SET Longitude='"
                                                    + lon
                                                    + "' "
                                                    + "WHERE Longitude="
                                                    + "'" + y + "'" + "");
                                    System.out.println("location updated");
                                    System.out
                                            .println("lat and lon values are"
                                                    + lat + lon);
                                    st.prepare();
                                    oops.prepare();
                                    st.execute();
                                    oops.execute();
                                    st.close();
                                    oops.close();
                                    sqliteDB.close();

                                } catch (Exception e) {
                                    System.out.println(e.getMessage());
                                    e.printStackTrace();
                                }

                            }
                        });
            }

        }
    }
}

public void providerStateChanged(LocationProvider provider, int newState) {
    if (newState == LocationProvider.OUT_OF_SERVICE) {
        // GPS unavailable due to IT policy specification
        System.out.println("GPS unavailable due to IT policy specification");
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert("GPS unavailable due to IT policy specification");

                return;
            }
        });
    } else if (newState == LocationProvider.TEMPORARILY_UNAVAILABLE) {
        // no GPS fix
        System.out.println("GPS temporarily unavailable due to IT policy specification");
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert("no GPS fix");
                return;
            }
        });
    }

}


public ApplicationIndicator _indicator; 
public static handleGPSListener _instance;

public void setupIndicator() {

    //Setup notification 
    if (_indicator == null) {
        ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
        _indicator = reg.getApplicationIndicator();

        if(_indicator == null) {
            ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("daslogo.png"));
            _indicator = reg.register(icon, false, true);  
            _indicator.setValue(0);
            _indicator.setVisible(false);
        }
    }

}

public void setVisible(boolean visible, int count) {

    if (_indicator != null) {
        if (visible) {
            _indicator.setVisible(true);
            _indicator.setValue(count);
        } else {
            _indicator.setVisible(false);
        }
    }

}

handleGPSListener () {

}

public static handleGPSListener getInstance() {
    if (_instance == null) {
        _instance = new handleGPSListener ();
    }
    return(_instance);
}
public double returnLong(){
    return lon;
}
public double returnLat(){
    return lat;
}

}

1 个答案:

答案 0 :(得分:1)

你的处理程序的locationUpdated方法永远不会被调用,对吗?如果你直接调用getLocation会有效吗?

我无法让侦听器正常工作,最终转而使用计时器代替我调用getLocation ......

我怀疑侦听器只是侦听事件并且不创建它们,即如果有人询问了该位置,那么听众也会收到它,但是如果没有人要求你获得该位置什么都没有。

在GPS中,永远不要相信模拟器是明智之举。 :)