Android 在我启动google maps之前,这些函数不会返回设备的位置

Android 在我启动google maps之前,这些函数不会返回设备的位置,android,location,android-alarms,java-threads,android-fusedlocation,Android,Location,Android Alarms,Java Threads,Android Fusedlocation,我创建了一个“splashscreen.java”类,其中有一个线程,其目的是在应用程序启动时检索设备的位置,该线程的源代码如下: private void initialize() { new Thread(() -> { if (Looper.myLooper() == null) { Looper.prepare(); } context.getMainLoope

我创建了一个“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分钟启动一个线程(其代码如下所示)

当我启动应用程序时,我通过断点检查是否实际检测到位置,但我注意到没有返回任何数据。但是,当我开始使用谷歌地图(GoogleMaps)时,这些函数就开始返回设备的位置。我做错了什么

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_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && 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_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && 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);
    }
}