启用位置服务后立即获取位置

时间:2017-01-17 12:22:36

标签: android locationlistener location-services

在用户启用位置服务后,我是否可以立即获得位置(纬度和经度)?使用我现在的代码,当用户启用位置服务时,我第一次获得0.0的纬度和经度,之后它工作正常。所以我的问题是如何在用户启用位置服务后立即获得经度和经度?

GPSTracker.java

public class GPSTracker implements LocationListener {

private Context context;
private double longitude;
private double latitude;
private LocationManager locationManager;
private Location location;
private boolean isGPSEnabled;
private boolean isNetworkEnabled;
private boolean canGetLocation;

public GPSTracker(Context context) {
    this.context = context;
    getLocation();
}

public Location getLocation() {
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!PermissionsManager.getInstance(context).checkLocationPermission((Activity) context)) {
        PermissionsManager.getInstance(context).requestLocationPermissions((Activity) context);
    } else {
        if (!isGPSEnabled && !isNetworkEnabled) {

            canGetLocation = false;
        } else {
            canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Constants.MIN_TIME_BETWEEN_UPDATES, Constants.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    updateGPSCoordinates();
                }
            }

            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Constants.MIN_TIME_BETWEEN_UPDATES, Constants.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        updateGPSCoordinates();
                    }
                }
            }
        }
    }

    return location;
}

public void updateGPSCoordinates() {
    if (location != null) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }
}

public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }
    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {

}

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

}

}

1 个答案:

答案 0 :(得分:0)

您可以通过{g}打开/关闭gps等提供商更改Broadcast Receiver来实现这一目标。 请按照以下代码进行实施。 在你的清单中,将接收器标签放在如下

<receiver android:name=".YourReceiverName">
<intent-filter>
    <action android:name="android.location.PROVIDERS_CHANGED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<强> Receiver.java

    public class YourBroadcastReceiver extends BroadcastReceiver {        
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
            Toast.LENGTH_SHORT).show();

        }
    }
}