位置捕获错误 - 延迟

时间:2017-11-27 11:33:57

标签: android

当我启动应用程序时,我会检查位置数据是否处于活动状态。否则,请将用户发送到GPS /本地设置屏幕。

问题:

当GPS已被禁用一段时间并启用时,应用程序中会出错。但如果你等几秒钟,它将正常工作。有一种延迟。

我的位置检查有什么问题?

我怎么能改善这个?

我检查的代码:

Code OnCreate:

        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

        getLocationPermission();

        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {

            //Avisar que nao tem GPS acionado
            //Toast.makeText(this, "Gps is not active....", Toast.LENGTH_SHORT).show();
            buildAlertMessageNoGps();

        } else {

            getDeviceLocation();

}

功能:

private void getDeviceLocation() {
        /*
         * Get the best and most recent location of the device, which may be null in rare
         * cases when a location is not available.
         */
        try {

            if (mLocationPermissionGranted) {
                Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
                locationResult.addOnCompleteListener(this, new OnCompleteListener<Location>() {
                    @Override
                    public void onComplete(@NonNull Task<Location> task) {
                        if (task.isSuccessful()) {

                            mLastKnownLocation = task.getResult();

                            /*FireStore*/
                            Map<String, Object> geoUser = new HashMap<>();
                            geoUser.put("Coordenadas", new GeoPoint(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()));
                            geoUser.put("Coordenadas_timestamp", FieldValue.serverTimestamp());

                            Task<Void> coordenadasUser = db.collection("Usuarios").document(mCurrentUser.getUid())
                                    .update(geoUser).addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {

                                            //OK

                                        }
                                    }).addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {

                                            Toast.makeText(HomeActivity.this, "Error!", Toast.LENGTH_SHORT).show();
                                        }
                                    });


                        } else {
                            Toast.makeText(HomeActivity.this, "Error", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

        } catch (SecurityException e)  {
            Log.e("Exception: %s", e.getMessage());
        }
}


private void getLocationPermission() {
        /*
         * Request location permission, so that we can get the location of the
         * device. The result of the permission request is handled by a callback,
         * onRequestPermissionsResult.
         */
        if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mLocationPermissionGranted = true;
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        }
    }

    /**
     * Handles the result of the request for location permissions.
     */
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        mLocationPermissionGranted = false;
        switch (requestCode) {
            case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mLocationPermissionGranted = true;
                }
            }
        }

}

谢谢!

0 个答案:

没有答案