首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓:删除LocationListener

安卓:删除LocationListener
EN

Stack Overflow用户
提问于 2013-01-11 09:25:26
回答 3查看 9.3K关注 0票数 2

我有下面的代码来获得用户使用GPS定位。(代码运行良好)

但我的问题是,当我关闭程序(按后退按钮)时,它会生成错误消息。

应用程序.已停止工作.........Please重试。

我认为我必须删除侦听器,因为我在onDestroy()中使用了onDestroy方法,因此也生成了相同的错误消息。

我怎么能解决这个问题。

我的代码:

代码语言:javascript
复制
public class Location_AddressActivity extends Activity {

    /** Called when the activity is first created. */
    TextView txt_logitude;
    TextView txt_latitude;
    TextView txt_address;
    LocationListener mlocListener;
    LocationManager mlocManager;
    Geocoder geocoder;

    public void onCreate(Bundle savedInstanceState) {       

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txt_logitude=(TextView)findViewById(R.id.txt_logitude);
        txt_latitude=(TextView)findViewById(R.id.txt_latitude);
        txt_address=(TextView)findViewById(R.id.txt_address);
        geocoder = new Geocoder(this, Locale.ENGLISH);
        txt_latitude.setText("");
        txt_logitude.setText("");
        txt_address.setText("");
        mlocManager= (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    }
    public  void onDestroy()
    {
        mlocManager.removeUpdates(mlocListener);
    }
    class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(Location location) {
            double latitude=location.getLatitude();
            double longitude=location.getLongitude();
            txt_latitude.setText(new Double(latitude).toString());
            txt_logitude.setText(new Double(longitude).toString());
            try {
                List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);              
                if(addresses != null) {
                    Address returnedAddress = addresses.get(0);
                    StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                    for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                    }
                    txt_address.setText(strReturnedAddress.toString());
                }
                else{
                    txt_address.setText("No Address returned!");
                }
            } 
            catch (IOException e) {
                e.printStackTrace();
                txt_address.setText(e.getMessage());
            }
        }
        public void onProviderDisabled(String provider) {
            txt_address.setText("GPS Disable");
        }
        public void onProviderEnabled(String provider) {
            txt_address.setText("GPS Enable");      
        }           
    }   
}

谢谢..

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-11 09:29:37

您已经删除了对活动的super.onDestroyonDestroy()中的调用。只需按如下所示修改该代码,并进行尝试。

代码语言:javascript
复制
public  void onDestroy()
    {
        super.onDestroy();
        mlocManager.removeUpdates(mlocListener);
    }

我希望removeUpdates()不要为您创建任何问题。

票数 15
EN

Stack Overflow用户

发布于 2013-01-11 09:33:49

代码语言:javascript
复制
location.removeUpdates(your listener);

这是删除位置侦听器的正确方法。

票数 2
EN

Stack Overflow用户

发布于 2013-01-11 09:44:49

我猜当您通过实现LocationListener类定制位置侦听器时,您应该创建MyLocationListener的实例,这里您注册的是超级类,您也在定制它-- .that没有意义。

尝试MyLocationListener mLocationListener在onCreate之上,并使用该对象进行请求和删除更新。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14275053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档