getLastKnownLocationが常にnullを返す理由を説明してください。getLastKnownLocationは常にnullを返します。説明してください?
これはのonCreateメソッドである:私は私のデバイス上でアプリケーションをテストしていし、デバイス上のGoogleの場所を有効にしている
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
:
@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");
}
ログはこれを示します。
問題はgetSystemService行(私も同じ問題があります)ですが、解決方法はわかりません。 – ollie299792458