我正在尝试通过单击按钮刷新当前位置。该位置可以通过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
课。 我将逐一解释我所做的主要更改:
-
尝试使用 Java 命名约定。 这使我们更容易为您提供帮助。在将代码发布到
handleeGPS
类之前,我认为它是一个变量,因为小写名称通常用于变量,而不是类。 -
避免重复代码。
handleeGPS
类有很多代码需要通读,但其中大部分是从Google的网络服务获取位置的代码,你在两个地方复制了这些代码。 只需创建一个仅包含该代码的方法,并调用它两次。 -
我把你的
handleeGPS
课改名为GPSHandler
. 我不确定handlee
是一个错误,或者这是否是您使用的另一种语言中的单词。 无论如何,名称至少应该以大写字母开头。 -
避免大量
static
变量和方法。 有时候,真的应该只有其中之一。 GPS 处理类可能是一个很好的例子,因为该设备只有一个 GPS 系统。 但是,要强制实施此代码构造,请不要将所有内容标记为static
。只需使类成为单例,它只涉及创建一个static
成员变量(_instance
)和一个静态方法(getInstance()
)。 在我的代码中,您将像这样访问该类:GPSHandler gps = GPSHandler.getInstance();
. -
我相信您对是否安装了BB地图的检查实际上是倒退的。 您查找了
net_rim_bb_lbs
模块,如果它大于零(这意味着安装了BB Maps),那么您直接转到Google网络服务。 我认为您希望以相反的方式(如果安装了BB地图,请尝试设备GPS)。 另外,从 6.0 开始,您也需要检查net_rim_bb_maps
,也是。 -
在您发布更新之前,我认为您的
getLatitude()
和getLongitude()
方法实际上是在获取设备位置。 这是我的一个糟糕的假设。 他们只是将数字转换为字符串。 因此,没有理由在后台完成此操作(带有Thread
)。 您已经编写了handleeGPS
类以使用后台线程,这很好。 一个后台线程就足够了。 使用位置信息的 UI 也不需要后台Thread
。 我更改了代码以添加GPSListener
接口。 该接口应由 UI 代码实现,以接收位置更新。 没有理由继续循环,询问位置是否不等于 {0.0, 0.0}。 这是低效的。 使用我的代码,当位置发生变化时,您只会收到通知。 -
原始代码不是线程安全的。
handleeGPS
latitude
和longitude
变量在后台线程上设置,并在 UI 线程上访问。 这不安全。 两个线程不应同时读取和写入同一段数据。 通过更改代码将位置数据推送到GPSListener
,它避免了这个问题。 -
我取消了您在
handleeGPS
类中的Dialog.alert()
代码注释,这些代码对您不起作用,因为不允许从后台进行 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
,这是一个接口,定义如下:
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();
}
}
有很多我们看不到的代码(例如 getLatitude()
、getLongitude()
、refreshDetails()
)。 所以,那里可能会出错。 另外,我在您发布的代码中没有看到任何加载屏幕,所以我不能说为什么没有显示。
但是,这里有一些看起来不对劲的地方:
synchronized (Application.getEventLock())
{
busyDialog.show();
}
如果您阅读此 BlackBerry 论坛问题,您会发现尝试从主 (UI) 线程同步应用程序事件锁可能会导致您的应用程序冻结。 始终在 UI 线程上调用 public void fieldChanged(Field field, int context)
方法,因为它是监视按钮单击的 UI 线程,并回调单击处理程序,如 fieldChanged()
。
您还可以阅读适用于应用程序的 BlackBerry API 文档,其中解释了getEventLock()
适用于工作线程(也称为后台)线程,而不是主线程(也称为 UI)。
因此,无需使用特殊技术在已在 UI 线程上运行的代码中获取事件锁。 代替上面的代码,只需执行以下操作:
busyDialog.show();
这两种技术:
synchronized (Application.getEventLock())
{
busyDialog.show();
}
或
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
busyDialog.show();
}
});
是从后台线程安全调用 UI 方法的方法。 但是,您不应在已知已在 UI 线程上运行的代码中使用它们。
尝试修复它,看看您的问题是否消失。
编辑:此外,您的代码在刷新位置之前会检查用户名和密码。 这真的是你想要的吗? 我认为这与您的问题无关,但通常,我不希望需要用户名或密码即可访问位置服务。 当然,我不知道您的应用程序,因此这实际上只是我的评论。
我认为你的答案很简单,这里的答案是错误的:
((longi == "0.0" || lati == "0.0") || (longi.length() == 0 || lati.length()==0));
您必须使用 String.equals()
而不是运算符==
。
第一次调用后,longi
和lati
具有"0.0"
值。但是==
会返回false
,因为它默认比较引用,它们是不同的,因为它是不同的对象。