Android LocationListener,带有地理编码的Toast,活动离开后不停止”;OnCreate";
我有一个简单的locationListener,它只使用GPS启动。在OnLocationChanged内部,我执行地理代码查找并显示Toast消息。问题是,一旦活动不再在前方,gps仍在接收更新并处理Toasto。我在onStart、onPause、onDestroy和onStop中有RemoveUpdate 知道我为什么不能停止这项服务吗Android LocationListener,带有地理编码的Toast,活动离开后不停止”;OnCreate";,android,gps,lifecycle,toast,locationlistener,Android,Gps,Lifecycle,Toast,Locationlistener,我有一个简单的locationListener,它只使用GPS启动。在OnLocationChanged内部,我执行地理代码查找并显示Toast消息。问题是,一旦活动不再在前方,gps仍在接收更新并处理Toasto。我在onStart、onPause、onDestroy和onStop中有RemoveUpdate 知道我为什么不能停止这项服务吗 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns
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();
}
}
您没有覆盖任何方法
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
注释添加到您正在重写的方法中hmmmmm,我想知道是否需要设置locationListener=null;还有?你可能会有一个非法的辩论例外。您必须从logcat中发布错误才能继续提供帮助。该消息会准确地告诉您问题所在。你必须调用super.onStart()
作为你的onStart()
方法中的第一件事很明显你还没有看过基本的android教程。你应该这样做,这些都是你应该知道的基本知识。事实上,我知道,我很惊讶这个应用程序在没有onStart()的情况下还能工作。我只是问了一个问题,不必问我知道/不知道的事情。@Kuli:除了onCreate()之外,不重写任何其他活动生命周期方法是完全可以接受的
@Override
protected void onStart(){
}