带有Geocoded Toast的LocationListener,一旦活动离开“OnCreate”,就不会停止

时间:2010-12-28 19:32:32

标签: android gps lifecycle toast locationlistener

我有一个仅使用GPS启动的简单locationListener。在OnLocationChanged内部,我执行地理编码查找并显示Toast消息。问题是一旦活动不再在前面,gps仍在接收更新并处理Toasto。我在onStart,onPause,onDestroy和onStop中有RemoveUpdates。

我知道为什么我不能停止这项服务?

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

这是试图回家时的LogCat

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 callback=android.app.ITransientNotification$Stub$Proxy@4375d1f0 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 callback=android.app.ITransientNotification$Stub$Proxy@43711b70 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 = com.kuli.example.android511.android511$GPSLocationListener@43762f40
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

1 个答案:

答案 0 :(得分:2)

你没有覆盖任何方法。

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

这些不是这些方法的方法签名。它应该是

@Override
protected void onStart(){

}

您应该习惯将@Override注释添加到您要覆盖的方法