Ich brauche den aktuellen Gerätestandort (lat/lng), aber "getCurrentLocation()" zeigt die ganze Zeit "Kein Provider" an, daher ist der Standort null. Ich nenne diese Methode in onCreate() und erklärt alle Berechtigungen in Manifest-Datei:Aktuellen Standort kann nicht abgerufen werden
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
Standortdienste auf realen Gerät sind ebenfalls aktiviert.
GetCurrentLocation() Java-Code:
public void getCurrentLocation() {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
provider = locationManager.getBestProvider(c, 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;
}
location = locationManager.getLastKnownLocation(provider);
if(location!=null)
{
double lng=location.getLongitude();
double lat=location.getLatitude();
Toast.makeText(this, Double.toString(lat) + "\n" + Double.toString(lng), Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "No provider", Toast.LENGTH_SHORT).show();
}
}
Wie kann ich es beheben?
P.S .: Ich habe viele ähnliche Fragen überarbeitet, kann aber keine passende Lösung finden.
Sie haben den Teil über die Erlaubnis kommentiert, haben Sie zumindest die Erlaubnis gegeben, die Einstellungen zu verwenden? (Android 5.0 und älter) – AxelH