gpt4 book ai didi

BlackBerry 通过 GPS 或 Cell Tower 刷新位置

转载 作者:行者123 更新时间:2023-12-04 23:06:06 24 4
gpt4 key购买 nike

我正在尝试通过单击按钮来刷新当前位置。该位置可以通过 GPS 或手机信号塔获取,无论哪个可用。我的问题是我从来没有看到“加载屏幕”。我知道当坐标保持为零时,它会立即出现/关闭。有人可以帮助我 - 我在下面做错了什么?

位置不会更新,加载屏幕也不会出现。在没有加载屏幕的情况下,通过多次单击“刷新按钮”,我确实得到了位置。下面是我处理点击“刷新按钮”的代码:

FieldChangeListener refreshImgListener = new FieldChangeListener() {   
public void fieldChanged(Field field, int context)
{
Thread backgroundWorker = new Thread(new Runnable() {
public void run() {
refreshCoordinates();
}
});
busyDialog.setEscapeEnabled(false);
busyDialog.show();
backgroundWorker.start();
}
};

还有我的 refreshCoordinates()方法如下:
public void refreshCoordinates() {
do
{
getLatitude(handleeGPS.latitude);
getLongitude(handleeGPS.longitude);
} while ((longi == "0.0" || lati == "0.0") || (longi.length() == 0 || lati.length()==0));

UiApplication.getUiApplication().invokeLater( new Runnable()
{
public void run ()
{
lblLatitude.setText(lati);
lblLongitude.setText(longi);
busyDialog.cancel();
}
} );
}

public static String getLatitude(double value)
{
lati= Double.toString(value);
return lati;
}
public static String getLongitude(double value)
{
longi= Double.toString(value);
return longi;
}

返回纬度和经度值的类:
public class handleeGPS{

static GPSThread gpsThread;
public static double latitude;
public static double longitude;

public handleeGPS(){
gpsThread = new GPSThread();
gpsThread.start();
}

private static class GPSThread extends Thread{
public void run() {
Criteria myCriteria = new Criteria();
myCriteria.setCostAllowed(false);
int m_bbHandle = CodeModuleManager.getModuleHandle("net_rim_bb_lbs");
if(m_bbHandle>0){
try {
int cellID = GPRSInfo.getCellInfo().getCellId();
int lac = GPRSInfo.getCellInfo().getLAC();
String urlString2 = "http://www.google.com/glm/mmap";
// Open a connection to Google Maps API
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(urlString2);
HttpConnection httpConn2;
httpConn2 = (HttpConnection)connDesc.getConnection();
httpConn2.setRequestMethod("POST");
// Write some custom data to Google Maps API
OutputStream outputStream2 = httpConn2.openOutputStream();//getOutputStream();
WriteDataGoogleMaps(outputStream2, cellID, lac);
// Get the response
InputStream inputStream2 = httpConn2.openInputStream();//getInputStream();
DataInputStream dataInputStream2 = new DataInputStream(inputStream2);
// Interpret the response obtained
dataInputStream2.readShort();
dataInputStream2.readByte();
int code = dataInputStream2.readInt();
//Dialog.alert(code+"");
if (code == 0) {
latitude= dataInputStream2.readInt() / 1000000D;
longitude=dataInputStream2.readInt() / 1000000D;
//Dialog.alert(latitude+"-----"+longitude);
dataInputStream2.readInt();
dataInputStream2.readInt();
dataInputStream2.readUTF();
} else {
System.out.println("Error obtaining Cell Id ");
}
outputStream2.close();
inputStream2.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
} else {
try {
LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria);
try {
Location myLocation = myLocationProvider.getLocation(300);
latitude = myLocation.getQualifiedCoordinates().getLatitude();
longitude = myLocation.getQualifiedCoordinates().getLongitude();
if(latitude==0.0 && longitude==0.0){
try {
int cellID = GPRSInfo.getCellInfo().getCellId();
int lac = GPRSInfo.getCellInfo().getLAC();
String urlString2 = "http://www.google.com/glm/mmap";
// Open a connection to Google Maps API
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(urlString2);
HttpConnection httpConn2;
httpConn2 = (HttpConnection)connDesc.getConnection();
httpConn2.setRequestMethod("POST");
// Write some custom data to Google Maps API
OutputStream outputStream2 = httpConn2.openOutputStream();
//getOutputStream();
WriteDataGoogleMaps(outputStream2, cellID, lac);
// Get the response
InputStream inputStream2 = httpConn2.openInputStream();
//getInputStream();
DataInputStream dataInputStream2 = new DataInputStream(inputStream2);
// Interpret the response obtained
dataInputStream2.readShort();
dataInputStream2.readByte();
int code = dataInputStream2.readInt();
//Dialog.alert(code+"");
if (code == 0) {
latitude= dataInputStream2.readInt() / 1000000D;
longitude=dataInputStream2.readInt() / 1000000D;
//Dialog.alert(latitude+"-----"+longitude);
dataInputStream2.readInt();
dataInputStream2.readInt();
dataInputStream2.readUTF();
} else {
System.out.println("Error obtaining Cell Id ");
}
outputStream2.close();
inputStream2.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
catch ( InterruptedException iex ) {
return;
}
catch ( LocationException lex ) {
return;
}
} catch ( LocationException lex ) {
return;
}
}
return;
}
}

private static void WriteDataGoogleMaps(OutputStream out, int cellID, int lac)
throws IOException {
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeShort(21);
dataOutputStream.writeLong(0);
dataOutputStream.writeUTF("en");
dataOutputStream.writeUTF("Android");
dataOutputStream.writeUTF("1.0");
dataOutputStream.writeUTF("Web");
dataOutputStream.writeByte(27);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(3);
dataOutputStream.writeUTF("");
dataOutputStream.writeInt(cellID);
dataOutputStream.writeInt(lac);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.flush();
}
}

最佳答案

好的,所以虽然我原来的答案是有效的,您发布的代码有一些不同的问题,所以我发布了第二个答案。有很多看起来不正确的东西,我只是重写了你的 handleeGPS类(class)。我将一一解释我所做的主要更改:

  • 尝试使用Java naming conventions .这使我们更容易为您提供帮助。在您将代码发布到您的 handleeGPS 之前类,我以为它是一个变量,因为小写名称通常用于变量,而不是类。
  • 避免重复代码。 handleeGPS类有很多代码要通读,但其中大部分是从 Google 的网络服务获取位置的代码,您在两个地方重复了这些代码。只需创建一个只包含该代码的方法,然后调用它两次。
  • 我重命名了你的 handleeGPS上课至GPSHandler .我不确定 handlee是一个错误,或者这是您使用的另一种语言中的一个词。无论如何,名称至少应该以大写字母开头。
  • 避免大量 static变量和方法。有时,真的应该只有其中之一。 GPS 处理类可能是一个很好的例子,因为该设备只有一个 GPS 系统。但是,要强制执行此代码结构,请不要将所有内容标记为 static .只需make the class a Singleton ,其中只涉及创建一个 static成员变量 ( _instance ) 和一个静态方法 ( getInstance() )。在我的代码中,您将像这样访问该类:GPSHandler gps = GPSHandler.getInstance(); .
  • 我相信您对是否安装了 BB map 的检查实际上是倒退的。您查找了net_rim_bb_lbs模块,如果它大于零(这意味着安装了 BB map ),那么您直接转到 Google 网络服务。我认为您希望反过来(如果安装了 BB map ,请尝试设备 GPS)。此外,从 6.0 开始,您需要检查 net_rim_bb_maps , 也。
  • 在您发布更新之前,我以为您的 getLatitude()getLongitude()方法实际上是在获取设备位置。这对我来说是一个糟糕的假设。他们只是将数字转换为字符串。因此,没有理由在后台执行此操作(使用 Thread )。你已经写了你的handleeGPS类使用后台线程,这很好。一个后台线程就足够了。使用位置信息的 UI 应该也不需要背景 Thread .我更改了代码以添加 GPSListener界面。该接口(interface)应该由您的 UI 代码实现,以接收位置更新。没有理由继续循环,询问位置是否不等于 {0.0, 0.0}。那是低效的。使用我的代码,您只会在位置发生变化时收到通知。
  • 原始代码不是线程安全的。 handleeGPS latitudelongitude变量在后台线程上设置,并在 UI 线程上访问。那不安全。两个线程不应该同时读取和写入同一条数据。通过更改代码将位置数据推送到 GPSListener ,它避免了这个问题。
  • 我取消了 Dialog.alert() 的注释您在handleeGPS 中的代码类,这对您不起作用,因为您不允许从后台进行 UI 调用。我用 UiApplication.getUiApplication().invokeLater() 包围了这些电话。使他们安全。

  • 要使用这个类,在你的 UI 代码的某个地方,你会这样做,而不是使用 Thread。运行您的 refreshCoordinates()方法:

    public void fieldChanged(Field field, int context) 
    // this is called when your location refresh button is clicked
    GPSHandler.getInstance().setListener(this);
    GPSHandler.getInstance().requestLocationUpdates();
    busyDialog.setEscapeEnabled(false);
    busyDialog.show();
    }

    ...

    public void onLocationReceived(Coordinates location) {
    lblLatitude.setText(Double.toString(location.getLatitude()));
    lblLongitude.setText(Double.toString(location.getLongitude()));
    busyDialog.cancel();
    }

    确保您放置该代码(上图)的类也是 implements GPSListener ,这是一个接口(interface),在这里定义:

    public interface GPSListener {
    public void onLocationReceived(Coordinates location);
    }

    最后是 GPSHandler :

    public class GPSHandler { 

    private GPSThread _gpsThread;
    private Coordinates _location;
    private boolean _gotLocation;
    private GPSListener _listener;

    /** this class will be a Singleton, as the device only has one GPS system */
    private static GPSHandler _instance;

    /** @return the Singleton instance of the GPSHandler */
    public static GPSHandler getInstance() {
    if (_instance == null) {
    _instance = new GPSHandler();
    }
    return _instance;
    }

    /** not publicly accessible ... use getInstance() */
    private GPSHandler() {
    }

    /** call this to trigger a new location fix */
    public void requestLocationUpdates() {
    if (_gpsThread == null || !_gpsThread.isAlive()) {
    _gpsThread = new GPSThread();
    _gpsThread.start();
    }
    }

    public void setListener(GPSListener listener) {
    // only supports one listener this way
    _listener = listener;
    }

    private void setLocation(final Coordinates value) {
    _location = value;
    if (value.getLatitude() != 0.0 || value.getLongitude() != 0.0) {
    _gotLocation = true;
    if (_listener != null) {
    // this assumes listeners are UI listeners, and want callbacks on the UI thread:
    UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() {
    _listener.onLocationReceived(value);
    }
    });
    }
    }
    }

    private class GPSThread extends Thread {

    private void getLocationFromGoogle() {
    try {
    int cellID = GPRSInfo.getCellInfo().getCellId();
    int lac = GPRSInfo.getCellInfo().getLAC();

    String urlString2 = "http://www.google.com/glm/mmap";

    // Open a connection to Google Maps API
    ConnectionFactory connFact = new ConnectionFactory();
    ConnectionDescriptor connDesc;
    connDesc = connFact.getConnection(urlString2);

    HttpConnection httpConn2;
    httpConn2 = (HttpConnection)connDesc.getConnection();
    httpConn2.setRequestMethod("POST");

    // Write some custom data to Google Maps API
    OutputStream outputStream2 = httpConn2.openOutputStream();//getOutputStream();
    writeDataGoogleMaps(outputStream2, cellID, lac);

    // Get the response
    InputStream inputStream2 = httpConn2.openInputStream();//getInputStream();
    DataInputStream dataInputStream2 = new DataInputStream(inputStream2);

    // Interpret the response obtained
    dataInputStream2.readShort();
    dataInputStream2.readByte();

    final int code = dataInputStream2.readInt();
    UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() {
    Dialog.alert(code + "");
    }
    });

    if (code == 0) {
    final double latitude = dataInputStream2.readInt() / 1000000D;
    final double longitude = dataInputStream2.readInt() / 1000000D;
    setLocation(new Coordinates(latitude, longitude, 0.0f));

    UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() {
    Dialog.alert(latitude+"-----"+longitude);
    }
    });

    dataInputStream2.readInt();
    dataInputStream2.readInt();
    dataInputStream2.readUTF();
    } else {
    System.out.println("Error obtaining Cell Id ");
    }
    outputStream2.close();
    inputStream2.close();
    } catch (Exception e) {
    System.out.println("Error: " + e.getMessage());
    }
    }

    private void tryGetLocationFromDevice() {
    _gotLocation = false;
    try {
    Criteria myCriteria = new Criteria();
    myCriteria.setCostAllowed(false);
    LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria);

    try {
    Location myLocation = myLocationProvider.getLocation(300);
    setLocation(myLocation.getQualifiedCoordinates());
    } catch ( InterruptedException iex ) {
    System.out.println(iex.getMessage());
    } catch ( LocationException lex ) {
    System.out.println(lex.getMessage());
    }
    } catch ( LocationException lex ) {
    System.out.println(lex.getMessage());
    }

    if (!_gotLocation) {
    getLocationFromGoogle();
    }
    }

    public void run() {
    int bbMapsHandle = CodeModuleManager.getModuleHandle("net_rim_bb_lbs"); // OS < 6.0
    int bbMapsHandle60 = CodeModuleManager.getModuleHandle("net_rim_bb_maps"); // OS 6.0+
    if (bbMapsHandle > 0 || bbMapsHandle60 > 0) {
    tryGetLocationFromDevice();
    } else {
    getLocationFromGoogle();
    }
    }
    }

    private void writeDataGoogleMaps(OutputStream out, int cellID, int lac) throws IOException {
    DataOutputStream dataOutputStream = new DataOutputStream(out);
    dataOutputStream.writeShort(21);
    dataOutputStream.writeLong(0);
    dataOutputStream.writeUTF("en");
    dataOutputStream.writeUTF("Android");
    dataOutputStream.writeUTF("1.0");
    dataOutputStream.writeUTF("Web");
    dataOutputStream.writeByte(27);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(3);
    dataOutputStream.writeUTF("");

    dataOutputStream.writeInt(cellID);
    dataOutputStream.writeInt(lac);

    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.flush();
    }

    }

    关于BlackBerry 通过 GPS 或 Cell Tower 刷新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11946774/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com