3

我可以使用 LocationListenerLocationManager从片段中获取用户位置吗?或者我是否需要LocationListener在底层活动中使用 from 并使用接口(或其他方法)将数据传输回片段?由于更改应用程序 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;
}
4

2 回答 2

1

我知道这已经很晚了,但我只花了 2 个多小时才找到一些代码。以下是我在 Androidx 上的实现方式。任何进一步的改进将不胜感激。

创建一个新的帮助类

package me.yokeyword.sample.CONSTANTS;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import static android.content.Context.LOCATION_SERVICE;

public class GPSHelper implements LocationListener {

    private static final String TAG = "GPSHelper";


public Location getCurrentLocation(Context mContext){
    int MIN_TIME_BW_UPDATES = 1000;
    int MIN_DISTANCE_CHANGE_FOR_UPDATES = 5;
    Location loc = null;
    Double latitude, longitude;

    LocationManager locationManager = (LocationManager) mContext
            .getSystemService(LOCATION_SERVICE);

    // getting GPS status
    Boolean checkGPS = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);

    // getting network status
    Boolean checkNetwork = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!checkGPS && !checkNetwork) {
        Toast.makeText(mContext, "No Service Provider Available", Toast.LENGTH_SHORT).show();
    } else {
        //this.canGetLocation = true;
        // First get location from Network Provider
        if (checkNetwork) {
            Toast.makeText(mContext, "Network", Toast.LENGTH_SHORT).show();
            try {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    loc = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                }

                if (loc != null) {
                    Log.d(TAG, "getCurrentLocation: " + loc.getLatitude() + ", " + loc.getLongitude());
                    return loc;
                }
            } catch (SecurityException e) {

            }
        }
    }
    // if GPS Enabled get lat/long using GPS Services
    if (checkGPS) {
        Toast.makeText(mContext, "GPS", Toast.LENGTH_SHORT).show();
        if (loc == null) {
            try {
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("GPS Enabled", "GPS Enabled");
                if (locationManager != null) {
                    loc = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (loc != null) {
                        latitude = loc.getLatitude();
                        longitude = loc.getLongitude();
                    }
                }
            } catch (SecurityException e) {

            }
        }
    }
    Location locErr = null;
    return locErr;
}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

    }
}

从您的 Fragment 调用以下代码:

    GPSHelper gpsHelper = new GPSHelper();
    Location loc = gpsHelper.getCurrentLocation(getContext());
    if (loc!=null){
        Log.d(TAG, "onCreateView: " + loc.getLatitude() + ", " + loc.getLongitude());
    }
于 2019-08-11T11:04:30.583 回答
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您向位置管理器注册之前,不会发生回调。

于 2013-02-26T21:46:10.950 回答