Dude
Dude

Reputation: 1302

Gps warning messages on Binder threads

I'm running a Gps positioning element from my main Activityon a different thread than the UI thread:

public class MyActivity extends Activity {
    ...
public Handler gpshandler = null;
public void onCreate(Bundle savedInstanceState) {
    ...
    Thread gpsThread = new Thread(new Runnable() {
        public void run() {     
            Looper.prepare();
            gpshandler = new Handler();
            gps = new GPSClass(MyActivity.this);
            Looper.loop();
        }
    }, "GPSThread");
    gpsThread.start();
}
...
@Override
protected void onDestroy() {
    super.onDestroy();
    gps.onPause();
    gpshandler.getLooper().quit();
}

My GPSClass is as follows:

public class GPSClass implements LocationListener, GpsStatus.Listener{
    private double latitute, longitude;
    private LocationManager locationManager;
    private String provider;
    private GpsStatus status;
    private boolean hasGPSFix = false;
    private Location lastLoc;
    private long lastLocTime;
    private boolean started = false;

public GPSClass(Context context) {
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationManager.addGpsStatusListener(this);

    start();
    started = true;
}

private void start() {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setCostAllowed(false);
    criteria.setSpeedRequired(false);
    criteria.setAltitudeRequired(false);

    if(hasGPSFix || !started) {
        provider = locationManager.getBestProvider(criteria, true);
    } else {
        provider = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) ? 
                LocationManager.NETWORK_PROVIDER : locationManager.getBestProvider(criteria, true);
    }

    if(provider != null){
        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            onLocationChanged(location);
        } else {
            latitute = 0.0;
            longitude = 0.0;
        }
    }
    computeLocation();
}

public void computeLocation(){
    locationManager.requestLocationUpdates(provider, 20000, 100, this);     
}

public void onPause(){
    locationManager.removeUpdates(this);
}


@Override
public void onLocationChanged(Location location) {
    if (location == null) return;

    lastLocTime = SystemClock.elapsedRealtime();

    latitute = location.getLatitude();
    longitude = location.getLongitude();

    lastLoc = location;             
}

@Override
public void onProviderDisabled(String provider) {
    start();
}

@Override
public void onProviderEnabled(String provider) {
    start();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { // not a good method, there are some issues with it not being called, don't rely on it!
    if(status != LocationProvider.TEMPORARILY_UNAVAILABLE){
        start();
    }
}

public double getLatitute() {
    return latitute;
}

public double getLongitude() {
    return longitude;
}

public String getProvider() {
    return provider;
}

@Override
public void onGpsStatusChanged(int event) {
    status = locationManager.getGpsStatus(status);
    switch (event) {
    case GpsStatus.GPS_EVENT_STARTED:
        // Do Something 
        break;
    case GpsStatus.GPS_EVENT_STOPPED:
        // Do Something 
        break;
    case GpsStatus.GPS_EVENT_FIRST_FIX:
        hasGPSFix  = true;
        // Do Something 
        break;
    case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
        if (lastLoc != null)
            hasGPSFix = (SystemClock.elapsedRealtime() - lastLocTime) < 10000;

        if (hasGPSFix) {

// Do something.
        } else { 
            start();
        }

        // Do Something
        break;
    }       
}
}

I use the gpshandler to perform the Looper.quit() method and get the locations using the get methods of my gps object.

This method works fine and I'm able get the info I want. However, I sometimes(half of the time) get the following warning messages:

Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {41ada040} sending message to a Handler on a dead thread
java.lang.RuntimeException: Handler     (android.location.LocationManager$GpsStatusListenerTransport$1) {41ada040} sending message to a Handler on a dead thread
at android.os.Handler.sendMessageAtTime(Handler.java:473)
at android.os.Handler.sendMessageDelayed(Handler.java:446)
at android.os.Handler.sendMessage(Handler.java:383)
at android.location.LocationManager$GpsStatusListenerTransport.onGpsStopped(LocationManager.java:1382)
at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:57)
at dalvik.system.NativeStart.run(Native Method)

or

Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {41ada040} sending message to a Handler on a dead thread
java.lang.RuntimeException: Handler     (android.location.LocationManager$GpsStatusListenerTransport$1) {41ada040} sending message to a Handler on a dead thread
at android.os.Handler.sendMessageAtTime(Handler.java:473)
at android.os.Handler.sendMessageDelayed(Handler.java:446)
at android.os.Handler.sendMessage(Handler.java:383)
at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1382)
at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:57)
at dalvik.system.NativeStart.run(Native Method)

These warnings don't influence the process but I'd like to understand what is happening... Using DDMS I found that these warnings happen on Binder threads. Does anyone know why this is happening and why it doesn't happen all the time? Thanks

Edit

I've dug down a bit and I've realised that when they occur, they can occur on all of the Binders but that they affect only two of them at a time. I'm not familiar with Binders, but could it be that there is a broadcasting of the status? I don't know how to know to which threads the Binders are attached and I've tried debugging, but for some reason I can't reproduce the warnings in debug mode. However, I set a debug Log in the onGpsStatusChanged method which reports the gps status received. Here is the output of the logcat. I've just edited it in order to show on which threads the messages occur.

12-12 09:32:18.133: W/MessageQueue(Binder_3): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:18.133: W/MessageQueue(Binder_3): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:18.133: W/MessageQueue(Binder_3):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:18.133: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:18.133: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:18.133: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:18.133: W/MessageQueue(Binder_3):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:18.133: W/MessageQueue(Binder_3):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:18.133: W/MessageQueue(Binder_3):   at android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:18.133: W/MessageQueue(Binder_3):   at dalvik.system.NativeStart.run(Native Method)
12-12 09:32:18.133: D/gps(GPSThread): got status 4   
12-12 09:32:18.133: W/MessageQueue(Binder_1): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:18.133: W/MessageQueue(Binder_1): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:18.133: W/MessageQueue(Binder_1):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:18.133: W/MessageQueue(Binder_1):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:18.133: W/MessageQueue(Binder_1):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:18.133: W/MessageQueue(Binder_1):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:18.133: W/MessageQueue(Binder_1):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:18.133: W/MessageQueue(Binder_1):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:18.133: W/MessageQueue(Binder_1):   at android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:18.133: W/MessageQueue(Binder_1):   at dalvik.system.NativeStart.run(Native Method)

12-12 09:32:19.128: W/MessageQueue(Binder_1): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:19.128: W/MessageQueue(Binder_1): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:19.128: W/MessageQueue(Binder_1):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:19.128: W/MessageQueue(Binder_1):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:19.128: W/MessageQueue(Binder_1):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:19.128: W/MessageQueue(Binder_1):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:19.128: W/MessageQueue(Binder_1):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:19.128: W/MessageQueue(Binder_1):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:19.128: W/MessageQueue(Binder_1):   at android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:19.128: W/MessageQueue(Binder_1):   at dalvik.system.NativeStart.run(Native Method)
12-12 09:32:19.128: D/gps(GPSThread): got status 4   
12-12 09:32:19.128: W/MessageQueue(Binder_5): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:19.128: W/MessageQueue(Binder_5): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:19.128: W/MessageQueue(Binder_5):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:19.128: W/MessageQueue(Binder_5):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:19.128: W/MessageQueue(Binder_5):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:19.128: W/MessageQueue(Binder_5):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:19.128: W/MessageQueue(Binder_5):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:19.128: W/MessageQueue(Binder_5):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:19.128: W/MessageQueue(Binder_5):   at  android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:19.128: W/MessageQueue(Binder_5):   at dalvik.system.NativeStart.run(Native Method)

12-12 09:32:20.123: W/MessageQueue(Binder_3): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:20.123: W/MessageQueue(Binder_3): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:20.123: W/MessageQueue(Binder_3):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:20.123: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:20.123: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:20.123: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:20.123: W/MessageQueue(Binder_3):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:20.123: W/MessageQueue(Binder_3):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:20.123: W/MessageQueue(Binder_3):   at android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:20.123: W/MessageQueue(Binder_3):   at dalvik.system.NativeStart.run(Native Method)
12-12 09:32:20.123: D/gps(GPSThread): got status 4   
12-12 09:32:20.123: W/MessageQueue(Binder_4): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:20.123: W/MessageQueue(Binder_4): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:20.123: W/MessageQueue(Binder_4):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:20.123: W/MessageQueue(Binder_4):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:20.123: W/MessageQueue(Binder_4):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:20.123: W/MessageQueue(Binder_4):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:20.123: W/MessageQueue(Binder_4):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:20.123: W/MessageQueue(Binder_4):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:20.123: W/MessageQueue(Binder_4):   at android.os.Binder.execTransact(Binder.java:367)

12-12 09:32:21.173: D/gps(GPSThread): got status 4   
12-12 09:32:21.173: W/MessageQueue(Binder_2): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:21.173: W/MessageQueue(Binder_2): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:21.173: W/MessageQueue(Binder_2):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:21.173: W/MessageQueue(Binder_2):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:21.173: W/MessageQueue(Binder_2):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:21.173: W/MessageQueue(Binder_2):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:21.173: W/MessageQueue(Binder_2):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:21.173: W/MessageQueue(Binder_2):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:21.173: W/MessageQueue(Binder_2):   at android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:21.173: W/MessageQueue(Binder_2):   at dalvik.system.NativeStart.run(Native Method)
12-12 09:32:21.178: W/MessageQueue(Binder_3): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:21.178: W/MessageQueue(Binder_3): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:21.178: W/MessageQueue(Binder_3):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:21.178: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:21.178: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:21.178: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:21.178: W/MessageQueue(Binder_3):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:21.178: W/MessageQueue(Binder_3):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:21.178: W/MessageQueue(Binder_3):   at android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:21.178: W/MessageQueue(Binder_3):   at dalvik.system.NativeStart.run(Native Method)

12-12 09:32:22.108: D/gps(GPSThread): got status 4   
12-12 09:32:22.113: W/MessageQueue(Binder_5): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:22.113: W/MessageQueue(Binder_5): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {425536f8} sending message to a Handler on a dead thread
12-12 09:32:22.113: W/MessageQueue(Binder_5):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:22.113: W/MessageQueue(Binder_5):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:22.113: W/MessageQueue(Binder_5):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:22.113: W/MessageQueue(Binder_5):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:22.113: W/MessageQueue(Binder_5):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:22.113: W/MessageQueue(Binder_5):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:22.113: W/MessageQueue(Binder_5):   at android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:22.113: W/MessageQueue(Binder_5):   at dalvik.system.NativeStart.run(Native Method)
12-12 09:32:22.113: W/MessageQueue(Binder_3): Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:22.113: W/MessageQueue(Binder_3): java.lang.RuntimeException: Handler (android.location.LocationManager$GpsStatusListenerTransport$1) {42602058} sending message to a Handler on a dead thread
12-12 09:32:22.113: W/MessageQueue(Binder_3):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
12-12 09:32:22.113: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-12 09:32:22.113: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-12 09:32:22.113: W/MessageQueue(Binder_3):   at android.os.Handler.sendMessage(Handler.java:383)
12-12 09:32:22.113: W/MessageQueue(Binder_3):   at android.location.LocationManager$GpsStatusListenerTransport.onSvStatusChanged(LocationManager.java:1406)
12-12 09:32:22.113: W/MessageQueue(Binder_3):   at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:89)
12-12 09:32:22.113: W/MessageQueue(Binder_3):   at android.os.Binder.execTransact(Binder.java:367)
12-12 09:32:22.113: W/MessageQueue(Binder_3):   at dalvik.system.NativeStart.run(Native Method)

Upvotes: 0

Views: 539

Answers (1)

Dude
Dude

Reputation: 1302

I don't fully understand what was really happening, but I have a vague idea... I was adding the GPS listener before having done Looper.loop() and before having fully initialised my GPSClass. Therefore, I'm guessing that sometimes the LocationProvider used by the LocationManager crashed because my listener class had no yet finished being initialised or because the looper hadn't started.

However, the listener was still added which may explain why I was still getting the updates. I still don't understand why I was getting the warnings on different Binders though... The solution I found was to add the Gps listener to the LocationManager in a method triggered by my activity's onResume() instead of in the onCreate and to do .removeGpsStatusListener() when the GPSClass performed the onPause().

Many thanks to you zapl for your input ;)

Upvotes: 1

Related Questions