Android 如何找到当前的城市

Android 如何找到当前的城市,android,Android,我已经尝试了很多代码来查找当前的城市,但没有一个能像模拟器一样在设备上工作。。。。他们给我的位置是空的 locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE

我已经尝试了很多代码来查找当前的城市,但没有一个能像模拟器一样在设备上工作。。。。他们给我的位置是空的

locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {

                        if (ActivityCompat.checkSelfPermission(this, 
  Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.

                        }
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
请给出这个代码为什么给我空值的解决方案

 private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
    String strAdd = "";
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
        String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
        String city = addresses.get(0).getLocality();
        String postalCode = addresses.get(0).getPostalCode();
        String one3 = addresses.get(0).getSubLocality();
        if (addresses != null) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder("");
            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                strReturnedAddress
                        .append(returnedAddress.getAddressLine(i)).append(
                        "\n");
            }
            strAdd = strReturnedAddress.toString();
            et_longitude.setText("" + LONGITUDE);
            et_latitude.setText("" + LATITUDE);
            et_street_no.setText(address);
            // et_street_name.setText(one3);
            et_surburb.setText(postalCode);
            et_city.setText(city);
        } else {
            Log.e("", "My Current location address No Address returned!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("", "My Current location address No Address returned!");
    }
    return strAdd;

}

通过这个方法,你可以得到当前的latlng

et_city。SettexCity是你当前的城市,但在这个方法中,我需要纬度和经度…在我使用的问题中,这个值为0。所以请尝试解决这些问题。表示如何查找lat long。获取位置空:是否添加权限??是的,亲爱的!!!!正如你们在我的问题中看到的,我也尝试了这段代码,但仍然不知道会发生什么?更好的方法是..你们应该使用GPSTracker类来获取当前的latlng
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    Log.e("TAG","Lat : " + location.getLatitude());
    Log.e("TAG","Lng : " + location.getLongitude());