0

J'ai eu une application qui fonctionne bien pour GetGeoLocation pour le niveau 23 de l'API (Utilisation de checkSelfPermission). Mais, alors je devais le rendre compatible avec le niveau de l'API 21. Ainsi, checkSelfPermission ne fonctionnait pas comme il a été introduite dans l'API de niveau 23.Erreur CheckPermissions pour LocationManager dans Android Studio

Alors, je l'ai changé le code pour accueillir ceci:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    CheckForCoarseLocationPermission(); 

} 
private void CheckForCoarseLocationPermission() 
{ 
    if (android.os.Build.VERSION.SDK_INT >= 23) 
    { 
     // ANDROID 6.0 AND UP! 
     boolean accessCoarseLocationAllowed = false; 
     try 
     { 
      // Invoke checkSelfPermission method from Android 6 (API 23 and UP) 
      java.lang.reflect.Method methodCheckPermission = Activity.class.getMethod("checkSelfPermission", java.lang.String.class); 
      Object resultObj = methodCheckPermission.invoke(this, Manifest.permission.ACCESS_COARSE_LOCATION); 
      int result = Integer.parseInt(resultObj.toString()); 
      if (result == PackageManager.PERMISSION_GRANTED) 
      { 
       accessCoarseLocationAllowed = true; 


      } 
     } 
     catch (Exception ex) 
     { 
     } 
     if (accessCoarseLocationAllowed) 
     { 
      Log.v("TAG","Granted"); 
      LocationManager locationManager= (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new LocationListener());//ERROR Happening Here!!!!!!!!!!! 
     } 
     try 
     { 
      // We have to invoke the method "void requestPermissions (Activity activity, String[] permissions, int requestCode) " 
      // from android 6 
      java.lang.reflect.Method methodRequestPermission = Activity.class.getMethod("requestPermissions", java.lang.String[].class, int.class); 
      methodRequestPermission.invoke(this, new String[] 
        { 
          Manifest.permission.ACCESS_FINE_LOCATION 
        }, 0x12345); 
     } 
     catch (Exception ex) 
     { 
     } 
    } 
} 

maintenant, le LocationManager.requestLocationUpdate donne une erreur appel nécessite l'autorisation qui peut être rejetée par l'utilisateur: code doit vérifier explicitement pour voir si l'autorisation est disponible (avec checkPermission) ou traiter explicitement un SecurityException potentiel moins

Comment y remédier, si je veux utiliser LocationManager et CheckPermission pour le niveau d'API> = 21.

Veuillez nous aider.

+0

http : //stackoverflow.com/questions/38141523/directory-creation-not-working-in-marshmallow-android/38141778#38141778 –

+1

if (Mani fest.permission.ACCESS_FINE_LOCATION == PackageManager.PERMISSION_GRANTED || Manifest.permission.ACCESS_COARSE_LOCATION == PackageManager.PERMISSION_GRANTED) { locationManager.removeUpdates (GPSListener.this); } essayez ceci. – Hemina

Répondre

1

Essayez cette

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { // coarse permission is granted 
      // Todo 
} else { // permission is not granted, request for permission 
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) { // show some info to user why you want this permission 
     Toast.makeText(this, "Allow Location Permission to use this functionality.", Toast.LENGTH_SHORT).show(); 
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123 /*LOCATION_PERMISSION_REQUEST_CODE*/); 
    } else { 
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123 /*LOCATION_PERMISSION_REQUEST_CODE*/); 

    } 
} 

Note: - vous devez avoir la permission suivante dans le manifeste Android

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
+0

AWESOME !!!! Merci –

0

Vous pouvez mettre la bibliothèque de support pour utiliser ici comme ça,

if(android.support.v4.content.PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) 
     { 
... 
     }