正确关闭gps的三个步骤:
第一个步骤:
移除LocationListener
第二个步骤:
将LocationManager置为null
第三个步骤:
将LocationListener置为null
下面是正确关闭gps的代码
- if(_locationManager != null){
- _locationManager.removeUpdates(<span style="font-family: Arial, Helvetica, sans-serif;">_locationListener</span>);
- _locationManager = null;
- }
- if(_locationListener != null) {
- _locationListener = null;
- }
第一个原因:最常见的原因。你可能有意或无意的实例化了多个LocationManager。最简单的避免这个错误的方法是,将实例LocationManager这个代码放在onResume()事件里面,同时在onPause()方法中关闭它。仔细的控制被你打开的LocationManager并及时关闭它。当你在不同的Activity之间LocationManager,你一定要留心。如果LocationManager正在运行,你又实例化了一个,那么你将不能够如愿用代码关闭它,你就只能通过手动来关闭它。
第二个原因:可能是你手机上的其他的APP已导致这个LocationManager运行中,那么很显然,它将不会通过你的app来关闭,多次检查已消除这个可能性,确保你已经关闭其他所有的可能用到gps定位的APP。