Ich habe ein Problem, das ich einfach nicht herausfinden kann. Ich erstelle eine Anwendung, die Location Services mit Android benötigt. Die Anwendung muss ständig nach dem Standort fragen. Dieser Code hat seit Monaten funktioniert, und als ich ihn dann von meinem Telefon deinstallierte und neu installierte, kehrte der Standort plötzlich null zurück. HiergetLastLocation() immer Null nach der Neuinstallation
ist die LocationProvider-Klasse, die den Standort mit einem Google-API-Client greift:
public class LocationProvider implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
public abstract interface LocationCallback {
public void handleNewLocation(Location location);
}
public static final String TAG = LocationProvider.class.getSimpleName();
/*
* Define a request code to send to Google Play services
* This code is returned in Activity.onActivityResult
*/
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private LocationCallback mLocationCallback;
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public LocationProvider(Context context, LocationCallback callback) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationCallback = callback;
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(3 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
mContext = context;
}
public void connect() {
mGoogleApiClient.connect();
}
public void disconnect() {
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Location services connected.");
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
//----------------
//----------------
Log.i(TAG, "couldnt get location");
noLocationEnabledDialog();
/*LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
mLocationCallback.handleNewLocation(location); */
}
else {
/* Log.i(TAG, "couldnt get location");
noLocationEnabledDialog(); */
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
mLocationCallback.handleNewLocation(location);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
/*
* Google Play services can resolve some errors it detects.
* If the error has a resolution, try sending an Intent to
* start a Google Play services activity that can resolve
* error.
*/
if (connectionResult.hasResolution() && mContext instanceof Activity) {
try {
Activity activity = (Activity)mContext;
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(activity, CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
/*
* If no resolution is available, display a dialog to the
* user with the error.
*/
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
public void noLocationEnabledDialog(){
AlertDialog.Builder setLocationDialog = new AlertDialog.Builder(mContext);
setLocationDialog.setTitle(R.string.location_message_title);
setLocationDialog.setMessage(R.string.location_message);
/*Button takes user to settings*/
setLocationDialog.setPositiveButton(R.string.affirmative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(settingsIntent);
}
});
/*If no location, close the activity*/
setLocationDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do nothing....yet
}
});
setLocationDialog.show();
}
@Override
public void onLocationChanged(Location location) {
mLocationCallback.handleNewLocation(location);
Log.i(TAG, "New location received ");
} }
Ich habe auch versucht, auf Google Maps Drehen eine Stelle auf dem Gerät zu packen, in der Hoffnung, dass sie die Lage nutzen würde aus Maps gegriffen. Kurioserweise wurde das Standortsymbol vor der Neuinstallation in der oberen Leiste angezeigt und es wird nicht mehr angezeigt. Weiß jemand, warum die Neuinstallation der Anwendung diesen Fehler verursachen würde? Ich betreibe dies auf einem Samsung Galaxy S6 mit Android 6.0.1
Vielen Dank!
Does diese Hilfe: http://stackoverflow.com/a/16866830/1594776 – Yashasvi
Was ist das Ziel api für das Projekt? –
TargetSDKVersion in der Gradle-Datei ist 23. Wie würde dies einen Unterschied machen, wenn es vor 5 Stunden im wahrsten Sinne des Wortes auf meinem Handy funktionierte, aber jetzt nach der Neuinstallation nicht funktioniert? – cloudyGirrl