Android无法获取位置

时间:2017-01-20 20:47:43

标签: android google-maps-android-api-2

我正在尝试获取虚拟设备AVD的位置,此处的位置返回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) {

}

结果是所有时间日志信息:位置失败,在这种情况下解决方案是什么

2 个答案:

答案 0 :(得分:0)

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

编辑:

输入此image中的坐标。

答案 1 :(得分:0)

这里的问题就像@Florescu所提到的那样,当我第一次给模拟器维度时,它从未知状态开始,找出模拟器将其理解为一个运动但是当我再次运行它时它给了我正确的行为