2016-03-23 4 views
0

Ich habe in meiner Aktivität hinzugefügt-Location-Berechtigungsprüfung auf Laufzeit-, aber ich bekomme immer noch den Fehler, sagen mir, die Erlaubnissprüfung hinzufügen ... hier ist mein Code. In OnCreate() Ich habe hinzugefügt:hinzugefügte Position Erlaubnis Prüfung auf Laufzeit gibt immer noch Fehler

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      bPermissionGranted = checkLocationPermission(); 
     } 

In auf onConnected:

public void onConnected(Bundle bundle) { 

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (bPermissionGranted) { 
      Location location =  LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

      handleNewLocation(location); 

     } else { 

      Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

      handleNewLocation(location); 
     } 
    } 
} 

und die beiden Verfahren:

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 

public boolean checkLocationPermission() { 
    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. 

      //Prompt the user once explanation has been shown 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 


     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 

public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_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. 

       Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
       LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

       handleNewLocation(location); 


      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
       Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show(); 
      } 
      return; 
     } 

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

und markieren noch die Folge mit roten Fehler:

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

Antwort

0

Nun, Ihr Code ist etwas in Ordnung . Überprüfen Sie nicht die Erlaubnis in onCreate. Überprüfen Sie es in Ihrer onConnected() Methode, weil es möglich ist, dass der Benutzer die Erlaubnis noch nicht erteilt hat und onConnected() vor ausgeführt wird.

Wenn Sie das Berechtigungsresultat in onRequestPermissionsResult() erhalten, beginnen Sie, die Positionsaktualisierungen abzurufen, wenn das Ergebnis positiv ist.

Auch eine Beratung -

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 

Dieses Stück Code ist redundant. Android benötigt nur Laufzeit-Berechtigungen in Android M und höher, damit Ihr Berechtigungsüberprüfungscode nur ausgeführt wird, wenn die Android-Version über M liegt, sonst wird sie nicht ausgeführt.

+0

in onCreate: bPermissionGranted = checkLocationPermission() - nimmt mich tatsächlich auf den Dialog mit dem Benutzer (onRequestPermissionsResult) ... im bPermissionGranted mit dem Benutzerantwort initialisieren .. – Hilit

+0

so dass diese Antwort nicht lösen mein Problem – Hilit

+1

Ich weiß, du Kumpel 'initialisiert bPermissionGraned mit der Benutzerantwort, aber Ihr 'onConnected()' kann aufgerufen werden, bevor der Benutzer eine Antwort gibt. Dann haben Sie ein Problem, da bPermissionGranted in diesem Moment nicht initialisiert wird. Starten Sie deshalb Ihren Code in 'onConnected()'. –