在我启动谷歌地图之前,这些函数不会返回设备的位置

问题描述

我创建了一个“splashscreen.java”类,其中有一个线程,其目的是在应用程序启动时检索设备的位置,该线程的源代码如下:

private void initialize() {
        new Thread(() -> {
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }

            context.getMainLooper();
            FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
            if (ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                fusedLocationClient.getLastLocation().addOnSuccessListener(SplashScreen.this,location -> {
                    if (location != null) {
                        dbGest.insertIntoLastPositionKNown(location,context);
                    }
                });
            }

            runOnUiThread(() -> {
                //DO STUFF
            });
        }).start();
    }

应用程序“需要”知道必须不断更新的设备的位置,为此我创建了一个前台服务,该服务调用并初始化一个每 5 分钟启动一个线程的警报管理器(其代码如下所示)

private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest mLocationRequest;
private LocationCallback mLocationCallback;
private Location mCurrentLocation;

public RequestLocation(Context context) {
    this.context = context;
}

@Override
public void run() {
    try {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setNumUpdates(5);

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                mCurrentLocation = locationResult.getLastLocation();
                DbGest.getInstance(context).insertIntoLastPositionKNown(mCurrentLocation,context);
            }
        };

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest);
        builder.setNeedBle(false);
        builder.build();
        if (ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            mFusedLocationClient.getLastLocation().addOnSuccessListener(location -> {
                if (location != null) {
                    if (ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        mFusedLocationClient.requestLocationUpdates(mLocationRequest,mLocationCallback,Looper.myLooper());
                    }
                }
            }).addOnFailureListener(e -> wil.WriteFile("1)RequestLocation - Exception: " + e.getMessage(),context));
        }
        mFusedLocationClient.removeLocationUpdates(mLocationCallback);
    } catch (Exception e) {
        wil.WriteFile("2)RequestLocation - Exception: " + e.getMessage(),context);
    }
}

当我启动应用程序时,我检查断点是否实际检测到位置,但我注意到没有返回任何数据。但是,当我开始使用 Google Maps 时,函数开始返回设备的位置。我做错了什么?

解决方法

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

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

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