如何在开启GPS几秒钟后获取当前位置?

时间:2018-03-21 15:35:56

标签: java android android-gps

当我长时间打开GPS时,我可以获取当前位置,但是当我关闭然后打开GPS时,它会返回null作为当前位置。我需要刷新或重新安装应用程序才能使当前位置正常工作。几秒钟打开GPS后是否可以获取位置?

这是我的代码:

client = LocationServices.getFusedLocationProviderClient(this);

        LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
            finish();
        } else {


            if (ActivityCompat.checkSelfPermission(MapsActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {

                return;
            }
            client.getLastLocation().addOnSuccessListener(MapsActivity.this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {

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

                }
            });
        }

我发现我需要使用requestsLoctionUpdates,但我找不到任何教程解释我如何在新版本的android studio中进行操作(位置:11.8.0)。任何人都可以告诉我如何使用这段代码吗?

0 个答案:

没有答案
相关问题