在片段中使用 LocationListener

问题描述 投票:0回答:1

我可以使用 LocationListener 和

LocationManager
从片段中获取用户位置吗?或者我是否需要在底层活动中使用
LocationListener
并使用接口(或其他方法)将数据传输回片段?由于更改应用程序 UI,我正在将活动转换为片段。当我使用独立活动时,获取位置没有问题,但是,既然我已将活动制作成片段,则无法返回任何位置。

下面是我添加 LocationListener 的方法。它之前被声明为 LocationListener locationListener。

private void addLocationListener(){
    locationListener = new LocationListener(){

        @Override
        public void onLocationChanged(Location location) {
            GeoPoint userLoc = processNewLocation(location);
            if(userLoc != null){
                Log.d("USERLOC", userLoc.toString());
                                    //do something with the location    
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            } else {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            } else {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    };
}

注意:processNewLocation 只是将位置转换为 GeoPoint。它甚至没有走那么远,因为没有获得任何位置。

这是向位置管理器注册监听器的代码

public void addLocationManager(){
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
}

private void getLocationFromNetwork(){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener);
Log.d("GET NETWORK", "Listener registered");
}

private void getLocationFromGPS(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener);
Log.d("GET GPS", "Listener registered");
}

private Location getLastLocation(){
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return lastKnownLocation;
}
android fragment locationmanager locationlistener
1个回答
0
投票

在您上面发布的代码中,您永远不会用

LocationListener
注册
LocationManager
。您需要从
onProviderEnabled
创建代码中某处的
Fragment
代码中获取这样的代码:

locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

在您向位置管理器注册后,

onProviderEnabled
回调才会发生。

© www.soinside.com 2019 - 2024. All rights reserved.