2010-12-28 14 views
0

Ich habe einen einfachen locationListener, der GPS nur zum Starten verwendet. Inside OnLocationChanged Ich mache eine Geocode-Suche und zeige eine Toast-Nachricht an. Das Problem ist, sobald die Aktivität nicht mehr im Vordergrund ist, erhält das GPS noch Updates und verarbeitet den Toasto. Ich habe RemoveUpdates in onStart, onPause, onDestroy und onStop.LocationListener mit geocodiertem Toast, der nicht aufhört, sobald die Aktivität "OnCreate" verlässt

Irgendeine Idee, warum ich diesen Dienst nicht stoppen kann?

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mapView = (MapView) findViewById(R.id.mapView); 
    mc = mapView.getController(); 

    locationListener = new GPSLocationListener(); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);  

... 
...  

public void onStart(Bundle savedInstanceState) { 
    locationManager.removeUpdates(locationListener); 
    locationManager=null; 
} 

public void onPause(Bundle savedInstanceState) { 
    locationManager.removeUpdates(locationListener); 
    locationManager=null; 
} 

public void onDestroy(Bundle savedInstanceState) { 
    locationManager.removeUpdates(locationListener); 
    locationManager=null; 
} 

public void onStop(Bundle savedInstanceState) { 
    locationManager.removeUpdates(locationListener); 
    locationManager=null; 
} 

class GPSLocationListener implements LocationListener {  
    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 

      current_heading=location.getBearing(); 
      String current_bearing = headingToString2(location.getBearing()); 

      point = new GeoPoint(
       (int) (location.getLatitude() * 1E6), 
       (int) (location.getLongitude() * 1E6)); 

      // add marker 
      MapOverlay mapOverlay = new MapOverlay(); 
      mapOverlay.setPointToDraw(point); 
      List<Overlay> listOfOverlays = mapView.getOverlays(); 
      listOfOverlays.clear(); 
      listOfOverlays.add(mapOverlay); 

      mapView.setStreetView(showStreet); 
      // enable to show Satellite view 
      mapView.setSatellite(showSatellite);   
      // enable to show Traffic on map 
      mapView.setTraffic(showTraffic);   
      mapView.setBuiltInZoomControls(true); 

      mc.animateTo(point); 
      mc.setZoom(16); 

      address = ConvertPointToLocation(point); 
      Toast.makeText(getBaseContext(), address, Toast.LENGTH_LONG).show(); 
      Toast.makeText(getBaseContext(), address, Toast.LENGTH_LONG).cancel(); 
      }  
    } 

Hier ist die LogCat, wenn zu Hause

12-28 16:41:51.402: ERROR/LocationMasfClient(577): reverseGeocode(): GLS failed with status 20 
12-28 16:41:51.423: INFO/NotificationService(577): enqueueToast pkg=com.kuli.example.android511 [email protected] duration=1 
12-28 16:41:53.272: DEBUG/LocationMasfClient(577): getAddressFromProtoBuf(): Ignore feature 0,Lyndon Station 
12-28 16:41:53.282: INFO/NotificationService(577): enqueueToast pkg=com.kuli.example.android511 [email protected] duration=1 
12-28 16:41:55.533: DEBUG/dalvikvm(633): GC freed 715 objects/92504 bytes in 324ms 
12-28 16:41:57.883: INFO/ActivityManager(577): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.HOME} flags=0x10200000 comp={com.android.launcher/com.android.launcher.Launcher} } 
12-28 16:41:57.932: DEBUG/LocationManager(1191): removeUpdates: listener = [email protected]f40 
12-28 16:41:57.952: DEBUG/AndroidRuntime(1191): Shutting down VM 
12-28 16:41:57.952: WARN/dalvikvm(1191): threadid=3: thread exiting with uncaught exception (group=0x4000fe70) 
12-28 16:41:57.966: ERROR/AndroidRuntime(1191): Uncaught handler: thread main exiting due to uncaught exception 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191): android.app.SuperNotCalledException: Activity {com.kuli.example.android511/com.kuli.example.android511.android511} did not call through to super.onPause() 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2830) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2797) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2780) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at android.app.ActivityThread.access$2000(ActivityThread.java:112) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at android.os.Looper.loop(Looper.java:123) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at android.app.ActivityThread.main(ActivityThread.java:3948) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at java.lang.reflect.Method.invoke(Method.java:521) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 
12-28 16:41:57.972: ERROR/AndroidRuntime(1191):  at dalvik.system.NativeStart.main(Native Method) 
12-28 16:41:57.992: INFO/Process(577): Sending signal. PID: 1191 SIG: 3 
12-28 16:41:57.992: INFO/dalvikvm(1191): threadid=7: reacting to signal 3 
12-28 16:41:58.133: INFO/dalvikvm(1191): Wrote stack trace to '/data/anr/traces.txt' 
12-28 16:41:58.442: WARN/ActivityManager(577): Activity pause timeout for HistoryRecord{437b5170 {com.kuli.example.android511/com.kuli.example.android511.android511}} 
12-28 16:43:08.393: DEBUG/dalvikvm(631): GC freed 9811 objects/553776 bytes in 104ms 
+0

hmmmmm, ich frage mich, ob ich Location = null setzen müssen; auch? – Kuli

Antwort

2

Sie sind nicht alle Methoden überschreiben zu gehen versuchen.

public void onStart(Bundle savedInstanceState) { 
    locationManager.removeUpdates(locationListener); 
    locationManager=null; 
} 

public void onPause(Bundle savedInstanceState) { 
    locationManager.removeUpdates(locationListener); 
    locationManager=null; 
} 

public void onDestroy(Bundle savedInstanceState) { 
    locationManager.removeUpdates(locationListener); 
    locationManager=null; 
} 

public void onStop(Bundle savedInstanceState) { 
    locationManager.removeUpdates(locationListener); 
    locationManager=null; 
} 

Das sind nicht die Methodensignaturen für diese Methoden. Es sollte

@Override 
protected void onStart(){ 

} 

etc

Sie sollte dem Hinzufügen der @Override Anmerkung zu Methoden gewöhnen, die Sie überschreiben

+0

Gehirn Furz ... DANKE !!! – Kuli

+0

aber jetzt bekomme ich einen Fehler, wenn ich die Anwendung verlassen ... – Kuli

+0

Sie erhalten wahrscheinlich eine IllegalArgumentException http://developer.android.com/reference/android/location/LocationManager.html#removeUpdates%28android.app. PendingIntent% 29. Sie müssen den Fehler von Ihrem Logcat veröffentlichen, um mehr zu helfen. – Falmarri