在片段中使用LocationListener

时间:2013-02-26 20:43:36

标签: android fragment locationmanager locationlistener

我可以通过在片段中使用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;
}

2 个答案:

答案 0 :(得分: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回调。

答案 1 :(得分:0)

我知道这很晚了,但是我花了两个多小时才找到一些代码。这是我在Androidx上实现此操作的方式。任何进一步的增强将不胜感激。

创建一个新的助手类

async/await

从您的片段中调用以下代码:

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) {

    }
}