【发布时间】:2013-10-12 18:20:08
【问题描述】:
我有一个客户端-服务器应用程序,客户端发送“ping”,服务器用“pong”响应。收到“pong”后,客户端将其位置更新(GPS 数据)发送到服务器。在接收到位置更新后,服务器会发送一个“pong”并持续一段时间。 套接字(用于发送和接收消息)由客户端和服务器在单独的线程中创建。我在主线程中注册了 LocationListener。问题是,我没有从 GPS 获得任何更新。我通过运行一个单独的应用程序检查了 GPS,该应用程序显示了看到的卫星数量和第一次修复所用的时间。第一次修复大约需要 90 秒。
我遇到的问题与here 和here 提到的问题非常相似。还有here。
我的代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.getBestProvider(criteria, true);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled) {
Log.i(TAG,"PING: GPS not enabled");
} else {
Log.i(TAG,"PING: GPS enabled");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
Log.i(TAG,"PING: adding GPS status listener");
locationManager.addGpsStatusListener(PingActivity.this);
}
/*The server and client threads are started after this.*/
LocationListener如下:
LocationListener locListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "PING: onLocationChanged");
Log.i(TAG, "PING: location is " + location.getLatitude() + ", " + location.getLongitude());
}
如您所见,LocationListener 中只有两条日志语句,而这两条日志语句根本没有打印出来。是因为我有一个线程不断地监听更新并且永远不会调用 LocationListener 吗?我还尝试为 GPS 创建一个单独的活动并在启动客户端-服务器线程之前注册它。
这些是清单文件中的权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
我在 LogCat 中得到的消息是
duplicate add listener for uid
有人可以对此有所了解吗?谢谢。
【问题讨论】:
-
LocationListener的其他回调方法呢?他们会被触发吗?
-
不,它们都没有被触发。
-
“LocationListener locListener = new LocationListener()...”在哪里调用?