【问题标题】:LocationListener is never calledLocationListener 永远不会被调用
【发布时间】:2013-10-12 18:20:08
【问题描述】:

我有一个客户端-服务器应用程序,客户端发送“ping”,服务器用“pong”响应。收到“pong”后,客户端将其位置更新(GPS 数据)发送到服务器。在接收到位置更新后,服务器会发送一个“pong”并持续一段时间。 套接字(用于发送和接收消息)由客户端和服务器在单独的线程中创建。我在主线程中注册了 LocationListener。问题是,我没有从 GPS 获得任何更新。我通过运行一个单独的应用程序检查了 GPS,该应用程序显示了看到的卫星数量和第一次修复所用的时间。第一次修复大约需要 90 秒。

我遇到的问题与herehere 提到的问题非常相似。还有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()...”在哪里调用?

标签: android locationlistener


【解决方案1】:

您收到代码错误(从 android 源代码中提取)

 private void handleAddListener(int uid) {
     synchronized(mListeners) {
         if (mClientUids.indexOfKey(uid) >= 0) {
             // Shouldn't be here -- already have this uid.
            Log.w(TAG, "Duplicate add listener for uid " + uid);
            return;
        }   
        mClientUids.put(uid, 0);
        if (mNavigating) {
            try {
                mBatteryStats.noteStartGps(uid);
            } catch (RemoteException e) {
                Log.w(TAG, "RemoteException in addListener");
            }
        }
    }
}

mClientUids 被声明为

private final SparseIntArray mClientUids = new SparseIntArray();

而且,在您的代码中,

   locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
    locationManager.addGpsStatusListener(PingActivity.this);

在这里,您添加了两个不同的侦听器,这就是 Android 所抱怨的。

【讨论】:

  • 我在独立的 GPS 应用程序中使用了完全相同的方法来获取 GPS 信号。那个时候我没有得到“重复添加监听器”的方法。我根据您的建议(在当前应用程序中)注释掉了该行,但仍然收到相同的消息。我还卸载了我的独立 GPS 应用程序,但这也没有帮助。
  • 认为这可能是由于线程试图注册 LocationListener 两次,我通过添加互斥锁来控制对关键部分的访问(注册 LocListener)。仍然没有变化。
  • 由于某种原因,Android 定位系统两次获取您的 appid。你必须阻止它。而且,在调试时将您的准确性更改为低水平,这样您就会知道您的问题是代码本身还是提供者。
猜你喜欢
  • 2017-01-20
  • 2013-03-22
  • 2012-10-27
  • 2012-03-27
  • 2013-08-29
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
相关资源
最近更新 更多