1

I have a client-server application where the client sends a "ping" and the server responds back with a "pong". On receipt of the "pong", the client sends its location updates (GPS data) to the server. On receiving the location updates, the server sends a "pong" and this goes on for a while. The sockets (for sending and receiving messages) are created by the client and server in separate threads. I register the LocationListener in the main thread. The problem is that, I do not get any updates from the GPS. I checked the GPS by running a separate app that displays the number of satellites seen and the time taken for the first fix. It took about 90 seconds for the first fix.

The problem I have is very similar to the ones mentioned here and here. Also here.

My code is given below:

@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.*/

The LocationListener is as follows:

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());
}

As you can see, there are just two log statements in the LocationListener and these two log statements are not printed at all. Is it because I have a thread constantly listening for updates and the LocationListener is never invoked? I also tried creating a separate activity for the GPS and registering that before starting the client-server threads.

These are the permissions in the manifest file.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

The message I get in LogCat is

duplicate add listener for uid

Can someone throw some light on this? Thanks.

4

1 回答 1

-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 所抱怨的。

于 2013-10-06T01:09:25.883 回答