开发者

weired android error on GPS Manager

开发者 https://www.devze.com 2023-03-07 04:55 出处:网络
I am trying to find out this problem without much luck for last couple of days. basically my gps listener is registered like this for getting gps events:

I am trying to find out this problem without much luck for last couple of days. basically my gps listener is registered like this for getting gps events:

    private void registerForNotifications(int gpsActivationIntervalMillis) {
    // gps satellite based
    gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, gpsActivationIntervalMillis, AppSettings.getMinimumDistance(),gpsLocationListener);
    gpsLocationManager.addGpsStatusListener(gpsLocationListener);
}

and it unregisters with this code:

    private void unRegisterForNotifications() {
    gpsLocationManager.removeUpdates(gpsLocationListener);
    gpsLocationManager.removeGpsStatusListener(gpsLocationListener);        
}

This is done on a background service and runs perfectly. But when I try to change the interval by doing an unregister & register with different interval:

private void restart() { registerForNotification(interval); unRegisterForNotification(); }

, I run into this:

05-21 13:12:56.354: WARN/MessageQueue(7290): Handler{460f47a8} sending message to a Handler on a dead thread 05-21 13:12:56.354: WARN/MessageQueue(7290): java.lang.RuntimeException: Handler{460f47a8} sending message to a Handler on a dead thread 05-21 13:12:56.354: WARN/MessageQueue(7290): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:179) 05-21 13:12:56.354: WARN/MessageQueue(7290): at android.os.Handler.send开发者_运维技巧MessageAtTime(Handler.java:457) 05-21 13:12:56.354: WARN/MessageQueue(7290): at android.os.Handler.sendMessageDelayed(Handler.java:430) 05-21 13:12:56.354: WARN/MessageQueue(7290): at android.os.Handler.sendMessage(Handler.java:367) 05-21 13:12:56.354: WARN/MessageQueue(7290): at android.location.LocationManager$ListenerTransport.onLocationChanged(LocationManager.java:157) 05-21 13:12:56.354: WARN/MessageQueue(7290): at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:58) 05-21 13:12:56.354: WARN/MessageQueue(7290): at android.os.Binder.execTransact(Binder.java:288) 05-21 13:12:56.354: WARN/MessageQueue(7290): at dalvik.system.NativeStart.run(Native Method) Blockquote

Looks like some dead activity/handler problem, but isn't android supposed to handle this? How am I supposed to properly change the gps subscription process?


Ok, I found the reason for the problem. I had a restart button in my activity which could restart the service properly. But when my AlarmListener tried to restart it from background, it starts the problem. I missed the point that the 'register' method should always be called from a Looper Thread! Solution is simple, just post the restart job to a handler which is attached to the main thread instead of calling the restart method directly from the AlarmManager thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号