在不稳定的Android中获取GPS位置

问题描述

我有一个包含3个活动的应用程序。 MainActivity是显示给用户的初始屏幕。 SecondActivity是一项允许用户扫描QR码的活动。扫描代码后,将启动ThirdActivity。

ThirdActivity显示GPS位置,以便以后在应用程序中使用它。

为了处理位置,我创建了这个帮助器类:

public class LocationHelper {
    private static GeoLocation mLocation;

    private static LocationRequest mLocationRequest;

    private static long UPDATE_INTERVAL = 10 * 1000;  /* 10 secs */
    private static long FASTEST_INTERVAL = 2000; /* 2 sec */

    public static void start(Context context)
    {
        startLocationUpdates(context);
        getLastLocation(context);
    }

    @SuppressLint("MissingPermission")
    protected static void startLocationUpdates(Context context) {

        // Create the location request to start receiving updates
        mLocationRequest = new LocationRequest();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

        // Create LocationSettingsRequest object using location request
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest);
        LocationSettingsRequest locationSettingsRequest = builder.build();

        // Check whether location settings are satisfied
        // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
        SettingsClient settingsClient = LocationServices.getSettingsClient(context);
        settingsClient.checkLocationSettings(locationSettingsRequest);

        // new Google API SDK v11 uses getFusedLocationProviderClient(this)
        getFusedLocationProviderClient(context).requestLocationUpdates(mLocationRequest,new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        // do work here
                        onLocationChanged(locationResult.getLastLocation());
                    }
                },Looper.myLooper());
    }

    public static GeoLocation getLocation() {
        return mLocation == null ? new GeoLocation() : mLocation;
    }

    protected static void onLocationChanged(Location location) {
        if (mLocation == null)
            mLocation = new GeoLocation();

        mLocation.setLatitud(location.getLatitude());
        mLocation.setLongitud(location.getLongitude());
    }

    @SuppressLint("MissingPermission")
    protected static void getLastLocation(Context context) {
        // Get last known recent location using new Google Play Services SDK (v11+)
        FusedLocationProviderClient locationClient = getFusedLocationProviderClient(context);

        locationClient.getLastLocation()
                .addOnSuccessListener(location -> {
                    // GPS location can be null if GPS is switched off
                    if (location != null) {
                        onLocationChanged(location);
                    }
                })
                .addOnFailureListener(e -> {
                    Log.d("LocationHelper","Error trying to get last GPS location");
                    e.printStackTrace();
                });
    }
}

在MainActivity onCreate方法中,我正在调用LocationHelper.start(this);,以便开始获取位置。

在ThirdActivity onCreate方法中,我调用了GeoLocation location = LocationHelper.getLocation();

问题在于,getLocation在某些执行中会正确检索位置,而在其他执行时,getLocation会将经度和纬度都设置为0。

我的代码有什么问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...