【发布时间】: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函数的使用。
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);
}
}
这是实现 LocationListener 的 myLocationListener。我添加了一些额外的代码用于测试目的。吐司永远不会弹出,所以看起来好像这段代码从未执行过。如果有人能帮我解决这个问题,我将不胜感激。
谢谢!
新问题:
在等待响应的同时继续在此页面中进行开发后,我注意到位置服务需要大约一分钟才能真正开始工作。所以,现在我的问题是:我如何克服用户必须等待使用应用程序的障碍?我见过使用基于位置的内容的应用程序,并且不需要那么长时间。我知道有 getLastKnownLocation 功能,但如果用户在再次打开应用程序之前旅行了 50 英里怎么办?对此的任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: android service location locationmanager locationlistener