我不知道为什么,但有时候即使关闭了应用程序,LocationManager仍然在工作。
我在一个活动的onCreate方法中调用startGPS()(只有一个活动,我称之为StartActivity)。
protected void startGPS(){
try {
lmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
lmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} catch(Exception e) {
e.printStackTrace();
}
}
如果这个活动将被销毁(也就是当应用程序关闭时),我会调用endGPS()函数。
public void endGPS(){
try {
lmanager.removeUpdates(this);
lmanager=null;
} catch(Exception e) {
e.printStackTrace();
}
}
有一些想法和建议,我做错了什么?!
requestLocationUpdates()
两次。 - CommonsWare