Ich habe eine App, die vor 2 Wochen in Ordnung funktioniert. Heute habe ich den Code ausgeführt und einige Probleme festgestellt. Es scheint nicht in der Lage zu sein zu erkennen, dass mein GPS (Standortoption) beim Start ist. Wenn der Standort deaktiviert ist, werde ich beim Start eine Meldung anzeigen und ersuche, die Standortoption einzuschalten, nachdem ich ihn eingeschaltet habe, wird mein aktueller Standort angezeigt. Wenn ich die Anwendung jedoch verlasse und erneut öffne, wird mein aktueller Standort nicht angezeigt, obwohl die Standortoptionen noch aktiviert sind.Android GPS nicht in der Lage, meinen Standort zu bekommen
Noch einmal, es funktioniert, ich konnte immer meinen aktuellen Standort beim Start bekommen, aber ich weiß nicht, warum es nicht mehr funktioniert, da ich nichts geändert habe.
Der Standort location = locationManager.getLastKnownLocation (Anbieter) gibt immer null zurück.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
googleMap.setPadding(0, 0, 0, 90);
//loadSettings();
// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
}else{
showGPSDisabledAlertToUser();
}
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
changeLocation(point);
}
});
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
private void showGPSDisabledAlertToUser(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Goto Settings Page To Enable GPS",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}