Android 如何防止GPS更新?

Android 如何防止GPS更新?,android,gps,locationlistener,Android,Gps,Locationlistener,编译以下代码时,我遇到编译错误: Error:(154, 47) error: local variable locationListener is accessed from within inner class; needs to be declared final 在locationListener中的下一行: getDistance(locationA,locationListener ); lm.removeUpdates(locationL

编译以下代码时,我遇到编译错误:

Error:(154, 47) error: local variable locationListener is accessed from within inner class; needs to be declared final 
locationListener
中的下一行:

getDistance(locationA,locationListener );
                    lm.removeUpdates(locationListener);
如何防止GPS更新

 locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(final Location location) {
                Location net_loc, gps_loc;
                net_loc = location;
                while (net_loc != null) {
                    showCurrentLocation(net_loc);
                    Location locationA = new Location("A");
                    locationA.setLatitude(location.getLatitude());
                    locationA.setLongitude(location.getLongitude());
                    getDistance(locationA,locationListener );
                    lm.removeUpdates(locationListener);
                }
试一下

lm.removeUpdates(this);
而不是

lm.removeUpdates(locationListener);

还有一点要记住,一旦在代码中输入while循环,它将持续运行,直到
net\u loc==null
,因此
lm.removeUpdates(locationListener)
每次调用。

或者声明
locationListener
final在您拥有它的地方,或者将其声明移动到类范围。我这样做了,但到目前为止,它从未停止gps更新28个结果和答案。可能重复