Android 获取位置失败

Android Fails to get location

我正在尝试获取虚拟设备的位置 AVD 这里的问题是位置 return by null ,我已经向应用程序添加了权限,但仍然无法获取位置

public class MainActivity extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
String provider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    provider = locationManager.getBestProvider(new Criteria(), false);

    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.

        Log.i("Location Info","Permission Denied");
        return;
    }
    Location location = locationManager.getLastKnownLocation(provider);
    if (location != null) {
        Log.i("Location Info","Location Achieved ");

    } else {

        Log.i("Location Info", "Location Failed");
    }

}

@Override
protected void onResume() {
    super.onResume();
    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;
    }
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

@Override
protected void onPause() {
    super.onPause();
    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;
    }
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
      Double longitude = location.getLongitude();
      Double latitude = location.getLatitude();
      Log.i("location Info Lat",longitude.toString());
      Log.i("Location Info long",longitude.toString());
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

结果一直是 log info :Location Failed ,这种情况下如何解决

模拟器上的位置是 null,因为它不知道您上次发送的位置(新开始或其他)。您需要将坐标发送到模拟器。在真实设备上测试时效果更好。

编辑:

像这样输入坐标 image。

这里的问题正如@Florescu 所提到的,当我第一次给仿真器尺寸时,它从未知状态开始,发现仿真器理解这是一个运动但是当我再次 运行 它时它给了我正确的行为