繁体   English   中英

获取位置数据后,从LocationManager退订LocationListener

[英]Unsubscribe LocationListener from LocationManager after getting location data

由于某种原因,当我在requestLocationUpdates()方法之后添加removeUpdates()时,我根本没有获得任何位置数据。 我没有正确取消订阅吗?

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 5000, 1, mlocListener);
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 1, mlocListener);
mlocManager.removeUpdates(mlocListener);


public class MyLocationListener implements LocationListener
    {
        @Override
        public void onLocationChanged(Location loc)
        {
            loc.getLatitude();
            loc.getLongitude();
            String text = "My current location is: " +
            "Latitude = " + loc.getLatitude() +
            "Longitude = " + loc.getLongitude();
            System.out.println("FINISHED:");
            Toast.makeText( getApplicationContext(),
                    text,
                    Toast.LENGTH_SHORT).show();

        }
}

您可以尝试制作一种方法并在其中执行removeUpdates()。 并在获取数据后从onLocationChanged()调用它。

可能会有所帮助。

获取位置数据退订,这意味着在onLocationChanged方法中。

我最终在我的LocationHandler内创建了一个带有嵌套的Thread while并一直循环直到找到GPS位置。 找到后,我取消了LocationListener订阅。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM