LocationManager.getLastKnownLocation(String provider)有可能返回null,概率还挺高
findViewById(R.id.llMain).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AndPermission.with(MainActivity.this).runtime().permission(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION) .onGranted(new Action<List<String>>() { @Override public void onAction(List<String> data) { boolean fineLocationPermissionGranted = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; boolean coarseLocationPermissionGranted = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED; if (fineLocationPermissionGranted && coarseLocationPermissionGranted) { LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location == null) { new AlertDialog.Builder(MainActivity.this).setMessage("无法获取位置").show(); } else { double longitude = location.getLongitude(); double latitude = location.getLatitude(); Log.d(TAG, longitude + ", " + latitude); new AlertDialog.Builder(MainActivity.this).setMessage("经度:" + longitude + " 纬度:" + latitude).show(); } } } }) .onDenied(new Action<List<String>>() { @Override public void onAction(List<String> data) { new AlertDialog.Builder(MainActivity.this).setMessage("位置权限被拒绝").show(); } }).start(); } });
以下是通过requestSingleUpdate获取经纬度,不过,LocationManager.requestSingleUpdate是异步的,获取到Location的时间不好控制,如果对经纬度的要求不是必须的,还是建议使用第一种
findViewById(R.id.btnGetLongitudeAndLatitude).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AndPermission.with(MainActivity.this).runtime().permission(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION) .onGranted(new Action<List<String>>() { @Override public void onAction(List<String> data) { boolean fineLocationPermissionGranted = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; boolean coarseLocationPermissionGranted = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED; if (fineLocationPermissionGranted && coarseLocationPermissionGranted) { LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, new LocationListener() { @Override public void onLocationChanged(Location location) { double longitude = location.getLongitude(); double latitude = location.getLatitude(); Log.d(TAG, longitude + ", " + latitude); new AlertDialog.Builder(MainActivity.this).setMessage("经度:" + longitude + " 纬度:" + latitude).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }, null); } else { new AlertDialog.Builder(MainActivity.this).setMessage("位置权限被拒绝").show(); } } }) .onDenied(new Action<List<String>>() { @Override public void onAction(List<String> data) { new AlertDialog.Builder(MainActivity.this).setMessage("位置权限被拒绝").show(); } }).start(); } });