Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android应用程序中使用LocationListener时,GPS坐标始终为0.0-0.0_Java_Android_Gps - Fatal编程技术网

Java 在Android应用程序中使用LocationListener时,GPS坐标始终为0.0-0.0

Java 在Android应用程序中使用LocationListener时,GPS坐标始终为0.0-0.0,java,android,gps,Java,Android,Gps,我需要为一项大学任务开发一个android应用程序,其中一部分包括将设备的GPS坐标发送到我的数据库。这是我用来获取坐标的类 public class MyLocationListener implements LocationListener { double latitude; double longitude; public MyLocationListener(Context myContext){ LocationManager locationManager = (L

我需要为一项大学任务开发一个android应用程序,其中一部分包括将设备的GPS坐标发送到我的数据库。这是我用来获取坐标的类

public class MyLocationListener implements LocationListener {

double latitude;
double  longitude;


public MyLocationListener(Context myContext){
    LocationManager locationManager = (LocationManager) myContext.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

public  double getLatitude(){
    return latitude; }

public  double getLongitude(){
    return longitude; }

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

   latitude =  location.getLatitude();
    longitude =  location.getLongitude();

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
}

在InterfaceActivity.java内部调用,使用以下方法:

        MyLocationListener ggwp = new MyLocationListener(InterfaceActivity.this);
    HttpDevices elenco = new HttpDevices(LoginIstance.getIst().getLog()[0],LoginIstance.getIst().getLog()[1],LoginIstance.getIst().getID(),String.valueOf(ggwp.getLatitude()),String.valueOf(ggwp.getLongitude()));
最后,HttpDevices是一个asynttask,它使用以下代码“运行”http消息到我的数据库:

            HttpPost httppost = new HttpPost("http://88.116.86.82/android/remote/connessione.php?user="+usr+"&pass="+pss+"&id="+id+"&lat="+lat+"&long="+log);
但是发送到数据库的coords总是0.0-0.0,我甚至不能在emulator(blue stack)上尝试代码并调试它,因为它没有gps coords。在我的摩托G上试过,但它们只有0。我试着在我的浏览器中滥发那个字符串(connessione.php等等),给出lat和long的随机值,是的,它是有效的。它更新了数据库并显示了我的应用程序中的坐标,所以问题应该出在java中。有什么建议吗?我在google/stackoverflow中搜索了很多,但什么也没找到,这是我第一次遇到需要我在这里发布的问题。希望你能帮助我!谢谢你

编辑:如果您想查看我的全部代码,有一个github链接:

我只是想通过你的代码。。。如果我遗漏了什么,请纠正我

如果我看一下你的HttpDevices类,你会尝试像这样发送数据

lat = String.valueOf(MyLocationListener.latitude);
log = String.valueOf(MyLocationListener.longitude);
这将尝试获取类属性的值,该值为0.0,因为更新lat和long的不是MyLocationListener的实例。。。您需要创建MyLocationListener的单例以获得侦听器的单例sharedInstance,或者您必须使用委托模式来执行此操作,或者您将值保存到SharedReferences

您是否有足够的Java知识来构建这些范例之一,并且对您有意义吗?(对不起,我不知道你有多好,伙计:-)否则我可以给你写点东西来解决这个问题

更新

这个问题看起来很棘手,所以我们需要一个“NastyLocationListener”来解决它,感谢Neo编写了最终的一个;-)

结帐这门课

就这样

/////////Just to know it
        Log.e("lati", NastyLocationListener.getLatitude(this)); //On most phones you can use the Listener without any other Code
        Log.e("longi", NastyLocationListener.getLongitude(this)); //It uses Androids native cached location but this is not updated proper
        /////////Just to know it end

        /////////Normal usage
        nastyLocationListener = new NastyLocationListener(this); //In your MainActivity instanciate a NastyLocationListener (its only needed once... All other Activities could get data from him)
        nastyLocationListener.startListening(); //It starts listening to Location Updates and triggers the Location Provider to fetch updates (its bool, true if almost one Provider is available, false if no Provider is available)

        nastyLocationListener.stopListening(); //Use this line when you pause the app, or quit it... it will stop the listener and could be resumed with startListening()

        NastyLocationListener.getLatitude(this); //Could be used in every class, on every time, however you want without any other code like above
        NastyLocationListener.getLongitude(this); //Could be used in every class, on every time, however you want without any other code like above
        /////////Normal usage end
更新

在我的设备上你的应用程序的屏幕截图,没有更改任何代码而不是REST调用,我已插入日志以输出我的纬度


试试这个类,它来自我的github帐户:

将其导入项目并创建对象:

GPSTracker gpstracker = new GPSTracker(context);
在那之后:

  gpstracker.getLatitude();
  gpstracker.getLongitude();

只需确保您设备的GPS已激活。希望这会有所帮助。

我的错,我没有在github上更新项目:(你能再检查一遍吗?不管怎样,是的,我知道什么是单例或设计模式,谢谢你的好意和时间好的,我明天会查看你的更新代码,你能给我写一条评论作为提醒吗?所以当我明天打开StackOverflow时,我会在顶部看到你的红旗…否则可能会至少我忘了…如果你在那之前解决了这个问题,请给我写一个简短的信息好吗?我还没有解决它,这是你的提醒!我会试试你上面那个人的代码谢谢你的提醒…我只是看了你的更新代码,现在它没有设置纬度和经度?!:-)我会给你一个讨厌的解决方法…:-)给我一点时间嘿,我不想打扰你,但是你看起来很好很善良,也许你想在skype上帮我一下?:)在这种情况下,这就是我的联系人:calannapTake看看我的屏幕截图。。。你的应用程序/我的侦听器正在工作,但你的设备没有位置,需要很长时间才能获取当前位置,但它可以工作…:-)