GPS는

2013-12-10 2 views
0

내가 UI 스레드가 아닌 다른 스레드에서 내 주요 Activity에서 GPS 위치 요소를 실행하는거야 스레드GPS는

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

gpshandler를 사용하여 Looper.quit() 메서드를 수행하고 gps 객체의 get 메서드를 사용하여 위치를 가져옵니다.

이 방법은 정상적으로 작동하며 원하는 정보를 얻을 수 있습니다. 그러나, 가끔은 (시간의 절반은) 다음과 같은 경고 메시지를 얻을 :

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) 

또는

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) 

는 이러한 경고는 과정에 영향을주지 않습니다하지만 난 무슨 일이 일어나고 있는지 이해하고 싶습니다. .. DDMS 사용하기 이러한 경고는 바인더 스레드에서 발생합니다. 왜 이런 일이 일어나고 왜 항상 그런 일이 일어나지 않는지 아는 사람이 있습니까? 감사합니다

편집

나는 조금 파고했고 나는 그들이 발생할 때, 그들은 바인더의 모든 발생할 수 있다는 것을 깨달았다하지만 그들은 한 번에 두 그들에 영향을 미친다. 나는 바인더에 익숙하지 않지만, 지위의 방송이있을 수 있습니까? 바인더가 연결된 스레드를 알 수있는 방법을 모르며 디버깅을 시도했지만 어떤 이유로 디버그 모드에서 경고를 재생할 수 없습니다. 그러나 onGpsStatusChanged 메소드에 디버그 로그를 설정하여 수신 된 gps 상태를보고합니다. 다음은 logcat의 출력입니다. 메시지를 어느 스레드에서 표시하는지 보여주기 위해 편집했습니다.

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) 
+0

, 당신이 ['.removeGpsStatusListener()'(http://developer.android.com/reference/android/location 부릅니까) 당신에게

많은 감사는 입력 zapl /LocationManager.html#removeGpsStatusListener%28android.location.GpsStatus.Listener%29) 어딘가에 발생합니까? – zapl

+0

아니요,하지만 루퍼를 죽이기 전에 경고가 나타나 문제가없는 것 같습니다 ...하지만 Gps 신호가있을 때 removeGpsStatusListener()를 사용해야하는지 확인해야합니다. 잃어버린. 입력을 주셔서 감사합니다;) – Dude

+0

당신의 에러는'GpsStatusListenerTransport'라고합니다 ..'죽은 쓰래드 핸들러'와'Looper'는'Handler'를 정의하는 부분입니다 (메세지를 그것의 큐에 넣었습니다.).상태 업데이 트가 루퍼에게 말하고 죽은 채로 발견하려고하는 것처럼 나에게 들린다. – zapl

답변

1

내가 완전히 정말 무슨 일이 일어 났는지 이해하지 않는,하지만 난 Looper.loop()을 수행하는 데 전에 완전히 내 GPSClass를 초기화하는 데 전에 GPS 수신기를 추가했다 ... 막연한 생각을 가지고있다. 따라서 이 LocationManager에 의해 사용되는 경우가 있는데, 그 이유는 내 리스너 클래스가 아직 초기화되지 않았거나 루퍼가 시작되지 않았기 때문입니다.

그러나 수신기가 여전히 추가되어 어떤 이유에서 내가 업데이트를 받고 있는지 설명 할 수 있습니다. 나는 아직도 내가 비록 다른 Binders에 경고를 얻는 이유를 이해하지 ... 내가 찾은 솔루션은 onCreate 내 활동의 onResume() 대신 트리거하는 방법에 LocationManager에의 GPS 수신기를 추가하고 .removeGpsStatusListener()을 할 수 있었다 GPSClass가 onPause()을 수행했을 때 당신이 루퍼를 죽일 경우

관련 문제