你能解释为什么getLastKnownLocation总是返回null吗?getLastKnownLocation总是返回null。请解释?
这是onCreate方法:
@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.
return;
}
locationManager.requestLocationUpdates(provider, 400, 1, this);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.i("Location info", "location achieved");
} else
Log.i("Location info", "location failed");
}
日志显示此:
04-25 19:13:03.437 11755-11755/com.example.saurabh.locationdemo E/MultiWindowProxy: getServiceInstance failed!
04-25 19:13:03.462 11755-11755/com.example.saurabh.locationdemo I/Location info: location failed
我测试我的设备上的应用程序,并在设备上启用谷歌的位置。
问题是与getSystemService行(我有同样的问题),但不知道如何解决它。 – ollie299792458