2015-10-29 7 views
9

Meine app versucht, die Position des Geräts zuzugreifen und ich habe folgendes im AndroidManifest.xml enthalten:ACCESS_FINE_LOCATION Security trotz der Genehmigung in der Manifest-Datei angeben

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.app"> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <application> 
     <meta-data android:name="com.google.android.gms.version" /> 
    </application> 

</manifest> 

ich die GoogleApiClient.ConnectionCallbacks umgesetzt haben als den Standort für den Zugriff folgt Service:

public class BackgroundLocationService implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 

    private static final String TAG = BackgroundLocationService.class.getSimpleName(); 

    private static GoogleApiClient googleApiClient; 
    private static PendingIntent locationCallback; 

    public static int LOCATION_INTERVAL = 10000; 
    public static int FAST_INTERVAL = 5000; 

    @Override 
    public void onConnected(Bundle bundle) { 
     Log.i(TAG, "Connected to Google API"); 

     LocationRequest request = new LocationRequest(); 
     request.setInterval(LOCATION_INTERVAL); 
     request.setFastestInterval(FAST_INTERVAL); 
     request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

     LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, locationCallback); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.i(TAG, Integer.toString(i)); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Log.i(TAG, connectionResult.toString()); 
    } 
} 

Wenn ich den Standort-Service-Aufruf auslösen, wird die folgende Ausnahme ausgelöst:

java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations. 
    at android.os.Parcel.readException(Parcel.java:1599) 
    at android.os.Parcel.readException(Parcel.java:1552) 
    at com.google.android.gms.location.internal.zzi$zza$zza.zza(Unknown Source) 
    at com.google.android.gms.location.internal.zzk.zza(Unknown Source) 
    at com.google.android.gms.location.internal.zzl.zza(Unknown Source) 
    at com.google.android.gms.location.internal.zzd$7.zza(Unknown Source) 
    at com.google.android.gms.location.internal.zzd$7.zza(Unknown Source) 
    at com.google.android.gms.internal.zzlb$zza.zzb(Unknown Source) 
    at com.google.android.gms.internal.zzlf.zza(Unknown Source) 
    at com.google.android.gms.internal.zzlf.zzb(Unknown Source) 
    at com.google.android.gms.internal.zzli.zzb(Unknown Source) 
    at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source) 
    at com.localz.spotzpush.sdk.service.BackgroundLocationService.onConnected(BackgroundLocationService.java:45) 
    at com.google.android.gms.common.internal.zzk.zzh(Unknown Source) 
    at com.google.android.gms.internal.zzlg.zznU(Unknown Source) 
    at com.google.android.gms.internal.zzlg.onConnected(Unknown Source) 
    at com.google.android.gms.internal.zzli$2.onConnected(Unknown Source) 
    at com.google.android.gms.common.internal.zzj$zzg.zzpf(Unknown Source) 
    at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source) 
    at com.google.android.gms.common.internal.zzj$zza.zzt(Unknown Source) 
    at com.google.android.gms.common.internal.zzj$zzc.zzph(Unknown Source) 
    at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Ich verwende ein tatsächliches Gerät, um dieses Problem zu erzeugen, und habe mir den Kopf darüber zerbrochen, warum die Ausnahme ausgelöst wird.

+0

Was ist Ihre 'minSdkVersion' und' targetSdkVersion'? – JBirdVegas

+0

Hey @JBirdVegas, sie sind 15 und 23 bzw. – gyamana

Antwort

14

Das Problem wird durch ein neues Feature für Android 6.0 verursacht wurde.

Ab Android 6.0 (API-Stufe 23) gewähren Benutzer Berechtigungen für Anwendungen, während die App ausgeführt wird, nicht wenn sie die App installieren.

http://developer.android.com/training/permissions/requesting.html

um das Problem zu erhalten, wenn die Genehmigung nicht ausdrücklich gewährt wird, hatte ich einen Scheck setzen Standortbestimmungsdienst-Anrufe zu wickeln (gemäß der Google-Dokumentation, leicht modifiziert).

In der Google-Dokumentation erfahren Sie auch, wie Sie die erforderlichen Berechtigungen anfordern können.

12th Okt 2016

Hinzufügen in @Siddharth ‚s Update AKTUALISIERT eine hilfreiche Antwort für checkSelfPermission()

//For this example, run the check onResume() 
@Override 
public void onResume() { 

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { 

      // Show an expanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 

     } else { 

      // No explanation needed, we can request the permission. 
      // PERMISSION_REQUEST_ACCESS_FINE_LOCATION can be any unique int 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_ACCESS_FINE_LOCATION); 
     } 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case PERMISSION_REQUEST_ACCESS_FINE_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 
+1

Ich bekomme java.lang.SecurityException: Client muss ACCESS_FINE_LOCATION Erlaubnis haben, um PRIORITY_HIGH_ACCURACY Standorte anzufordern. Absturz mit Target SDK 22 auf Android 5.1 (Gerät ist S plus (GiONEE_WBL7511)) Irgendwelche Hinweise? – arpitgoyal2008

+0

Bitte überprüfen Sie meine Antwort und nehmen Sie entsprechende Korrekturen vor. Ich lösche meine Antwort, wenn Sie Korrekturen an Ihrer Antwort vornehmen können. – Siddharth

+0

@Siddharth, OK, vielen Dank dafür, werde ich jetzt meine Antwort aktualisieren – gyamana

0

Die Standardimplementation von LocationSource muss nicht notwendigerweise GPS verwenden, um genaue Positionen zurückzugeben.

Wenn ich entfernen Sie die Erlaubnis "ACCESS_FINE_LOCATION" in der Manifest-Datei, sobald ich versuche, um die Karte anzuzeigen, die App abstürzt

Dies liegt daran, die Standardimplementierung verwendet LocationClient mit LocationRequest.PRIORITY_HIGH_ACCURACY.

GoogleMap.setMyLocationEnabled wenn Sie nur ACCESS_COARSE_LOCATION Erlaubnis anfordern, aber wechselt zu .

Sehen Sie diese Android: Google Maps location with low battery usage

0

Für alle anderen, die einen Fehler mit gyamana Antwort auf Manifest.permission aufzunehmen. ACCESS_FINE_LOCATION Fehler. Stellen Sie sicher, dass Sie android.Manifest anstelle von my.app.package.Manifest verwenden.

2

Auszug aus der Google-Dokumentation. Scheint, als ob die am meisten akzeptierte Antwort in diesem Thread einige kritische Informationen über den Rückruf des asynchronen checkSelfPermission fehlt.

Bitte bestätigen. Wenn nicht, lösche ich diese Antwort.

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
       Manifest.permission.READ_CONTACTS) 
     != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
      Manifest.permission.READ_CONTACTS)) { 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
       new String[]{Manifest.permission.READ_CONTACTS}, 
       MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 
+0

Wie könnte eine Erlaubnis vom Dienst anfordern? – ajay

Verwandte Themen