0

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!

+0

Does diese Hilfe: http://stackoverflow.com/a/16866830/1594776 – Yashasvi

+0

Was ist das Ziel api für das Projekt? –

+0

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

Antwort

0

Nun, es scheint, ich habe meine eigene Frage beantwortet. Ich bin ein ernsthafter Derp. Marshmallow erfordert, dass einzelne Apps um Erlaubnis für den Standort und dergleichen bitten. Da die vorherige Installation eine andere Ziel-API hatte, brauchte sie das nicht und das Manifest reichte aus.

Ich brachte die Ziel-API auf 22 runter und das war es. In Zukunft werde ich um Erlaubnis fragen müssen. Diese Frage beantwortet es ganz gut:

Permission issues for location in android Marshmallow applicaton