【问题标题】:Android Location Services - LocationListener does not workAndroid 定位服务 - LocationListener 不起作用
【发布时间】:2016-05-21 23:27:26
【问题描述】:

老问题:

我正在尝试获取我的设备的位置坐标,并且我已经按照我在研究时在多个区域中找到的所有步骤进行了操作。我已经设置了一个 LocationManager 并使用了与 LocationListener 绑定的 requestLocationUpdates 函数。但是,LocationListener 没有响应。我已经尝试调试以及在外面走动以执行 onChangedLocation 函数,但没有任何反应。在调试时,我的 LocationManager 的 requestLocationUpdates 函数已执行,但 LocationListener 从未执行。

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    textView = (TextView) findViewById(R.id.textView);
    textView2 = (TextView) findViewById(R.id.textView2);
    locationListener = new myLocationListener();

    textView.setText("Longitude", TextView.BufferType.NORMAL);
    textView2.setText("Latitude", TextView.BufferType.NORMAL);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, locationListener);

requestLocationUpdates

以上是requestLocationUpdates函数的使用。

private class myLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        //Log.e("Latitude: ", "" + location.getLatitude());
        //Log.e("Longitude: ", "" + location.getLongitude());
        if(location != null)
        {
            textView.setText(Double.toString(location.getLongitude()), TextView.BufferType.NORMAL);
            textView2.setText(Double.toString(location.getLatitude()), TextView.BufferType.NORMAL);
        }
        else
        {
            textView.setText("No Location", TextView.BufferType.NORMAL);
            textView2.setText("No Location", TextView.BufferType.NORMAL);
        }
        Toast.makeText(getApplicationContext(),"onLocationChanged Success",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }
}

myLocationListener

这是实现 LocationListener 的 myLocationListener。我添加了一些额外的代码用于测试目的。吐司永远不会弹出,所以看起来好像这段代码从未执行过。如果有人能帮我解决这个问题,我将不胜感激。

谢谢!

新问题:

在等待响应的同时继续在此页面中进行开发后,我注意到位置服务需要大约一分钟才能真正开始工作。所以,现在我的问题是:我如何克服用户必须等待使用应用程序的障碍?我见过使用基于位置的内容的应用程序,并且不需要那么长时间。我知道有 getLastKnownLocation 功能,但如果用户在再次打开应用程序之前旅行了 50 英里怎么办?对此的任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: android service location locationmanager locationlistener


    【解决方案1】:

    每个向 GPS 发出位置请求的设备都必须等到 gps 硬件变热。等待时间因设备和您的住宿地点而异。如果您在建筑物内,此时间可能需要 1 分钟或更长时间。

    为避免等待,您可以使用 getLastKnownLocation 方法,如果返回 缓存 位置,则通过 getTime 方法检查位置的日期。确定自己,是您的场景的旧位置吗?

    如果位置太旧,您必须提出位置请求并等待。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2017-11-09
      • 2014-10-09
      相关资源
      最近更新 更多