2016-12-24 3 views
3

사용자 위치가 필요한 앱을 제작 중입니다. 나는 말한다 here에서 안드로이드 교육 문서를 다음입니다 :shouldShowRequestPermissionRationale과 requestPermissions의 차이점은 무엇입니까?

shouldShowRequestPermissionRationale 반환 우리는 지금 (위험 허가 ACCESS_FINE_LOCATION)

권한을 요청하기위한 근거로 UI를 표시할지 여부를 나타내는 부울 (문서 자체에서 촬영)이 코드 :

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 explanation 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. 
    } 
} 

[MY DOUBT]할까요 (아래) 코드없는이 부분

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

ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS) 

에 해당하는 경우, 우리는 UI를 표시해야합니다,

if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
      Manifest.permission.READ_CONTACTS)) { 
    //HERE ..... 

} 

내 말은 .. 여기 '의 경우'조건 내부에 우리는 UI를 표시합니다 작성자 :

ActivityCompat.requestPermissions(thisActivity, 
    newString[{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

내가 실수 한 곳을 설명해주십시오. 나는 여기 붙어있다. 미리 감사드립니다. 예를 들어 주시면 감사하겠습니다.

참고 : 물론 Android M에서 내 앱을 실행 중이며 대상 SDK가 23 이상입니다.

답변

11

,

shouldShowRequestPermissionRationale 반환 우리가 허가를 요청하기위한 근거로 UI를 표시해야합니다> 여부를 나타내는 부울. 염두에두고

Allow SnazzyApp to access your contacts ? //this is NOT our custom UI 

, 지금

:

이 UI는 우리의 장치 쇼 (아래 참조), NOT 대화 상자를 (우리가 예를 들어,에 AlertDialog를 표시 할 수 있습니다) 우리의 사용자 정의 UI입니다

shouldShowRequestPermissionRationale의 반환 값은 순서도에 표시된 것과 같습니다.또한

점에 유의 enter image description here, 따라서

When that user "denies" your permission by CHECKING "never ask again", ``shouldShowRequestPermissionRationale`` would still return ``false``. 

, 응용 프로그램이 이전에 출시 된 사용자가 확인하지 않고 권한을 "거부"경우에만

  • shouldShowRequestPermissionRationale는 true를 돌려줍니다 요약 "물어 보지 않을 다시".
  • 다른 경우 (앱이 처음 실행되거나 이전에 앱이 시작되었으며 사용자가 '다시 묻지 않음'을 선택하여 권한을 거부 한 경우) 반환 값은 false입니다.

구현

은의 우리를 위해 서로 다른 경우를 처리하는 PermissionUtils.java 파일을 만들 수 있습니다.

public class PermissionUtils { 

    private static final String TAG = "PermissionUtils"; 

    /* 
     Inside this shared_preference file, we will just store information 
     about whether the user had visited our app earlier or not. 
    */ 

    private static final String PREFS_FILE_NAME = "preference_permission"; 
    private static final String PREFS_FIRST_TIME_KEY = "is_app_launched_first_time"; 


    //an interface containing 5 methods 
    //...the scenario in which these callback will be called is written below each method declaration. 
    public interface PermissionAskListener { 


     void onPermissionGranted(); 
     /* 
      User has already granted this permission 
      The app must had been launched earlier and the user must had "allowed" that permission 
     */ 


     void onPermissionRequest(); 
     /* 
      The app is launched FIRST TIME.. 
      We don't need to show additional dialog, we just request for the permission.. 

     */ 


     void onPermissionPreviouslyDenied(); 
     /* 
      The app was launched earlier and the user simply "denied" the permission.. 
      The user had NOT clicked "DO NOT SHOW AGAIN" 
      We need to show additional dialog in this case explaining how "allowing this permission" would be useful to the user 
     */ 


     void onPermissionDisabled(); 
     /* 
      The app had launched earlier and the user "denied" the permission.. 
      AND ALSO had clicked "DO NOT ASK AGAIN" 
      We need to show Toask/alertdialog/.. to indicate that the user had denied the permission by checking do not disturb too... 
      So, you might want to take the user to setting>app>permission page where the user can allow the permission.. 


     */ 

    } 

    // preference utility methods 
    private static boolean getApplicationLaunchedFirstTime(Activity activity) { 
     SharedPreferences sharedPreferences = activity.getSharedPreferences(PREFS_FILE_NAME, MODE_PRIVATE); 
     return sharedPreferences.getBoolean(PREFS_FIRST_TIME_KEY, true); 
    } 

    private static void setApplicationLaunchedFirstTime(Activity activity) { 
     SharedPreferences sharedPreferences = activity.getSharedPreferences(PREFS_FILE_NAME, MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putBoolean(PREFS_FIRST_TIME_KEY, false); 
     editor.commit(); 
    } 


    private static boolean isRuntimePermissionRequired() { 
     return (Build.VERSION.SDK_INT >= 23); 
    } 

    public static void checkPermission(Activity activity, String permission, PermissionAskListener permissionAskListener) { 

     Log.d(TAG, "checkPermission"); 


     if (!isRuntimePermissionRequired()) { 
      /* 
       Runtime permission not required, 
       THE DEVICE IS RUNNING ON < 23, So, no runtime permission required.. 
       Simply call **** permissionAskListener.onPermissionGranted() **** 
      */ 


      permissionAskListener.onPermissionGranted(); 
     } else { 
      //runtime permission required here... 

      //check if the permission is already granted, i.e the application was launched earlier too, and the user had "allowed" the permission then. 
      if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) { 
       /* We don't have permission, two cases arise: 
        1. App launched first time, 
        2. App launched earlier too, and the user had denied the permission is last launch 
          2A. The user denied permission earlier WITHOUT checking "Never ask again" 
          2B. The user denied permission earlier WITH checking "Never ask again" 
       */ 

       if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { 

        /* 
         shouldShowRequestPermissionRationale returned true 
         this means Case: 2A 
         see the flowchart, the only case when shouldShowRequestPermissionRationale returns "true", is when the application was launched earlier too and the user had "denied" the permission in last launch WITHOUT checking "never show again" 
        */ 

        permissionAskListener.onPermissionPreviouslyDenied(); 
       } else { 
        /* 
         this means, either - 
         Case: 1 or Case 2B 
         See Flowchart, shouldShowRequestPermissionRationale returns false, only when app is launched first time (Case: 1) or app was launched earlier too and user HAD checked "Never show again" then (Case: 2B) 
        */ 
        if (getApplicationLaunchedFirstTime(activity)) { 

         //Case: 1 
         Log.d(TAG, "ApplicationLaunchedFirstTime"); 

         setApplicationLaunchedFirstTime(activity); // ** DON'T FORGET THIS ** 
         permissionAskListener.onPermissionRequest(); 

        } else { 
         //Case: 2B 
         Log.d(TAG, "onPermissionDisabled"); 

         permissionAskListener.onPermissionDisabled(); 
        } 
       } 


      } else { 
       Log.d(TAG, "Permission already granted"); 

       permissionAskListener.onPermissionGranted(); 
      } 
     } 
    } 
} 

논리

  1. 우리는 먼저 우리가 심지어 처음에 실행 권한이 필요합니까 선택하여 시작

    ? 우리는 권한이없는 경우 우리가

    ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) 
    
  2. 이미 이전에 해당 권한을 가지고 있었다이라면, 우리는 실행 권한을 필요로 할 경우

    if (!isRuntimePermissionRequired()) {...} 
    
  3. , 우리가 확인 :이은에 의해 이루어집니다

    1. App launched first time, 
    2. App launched earlier too, and the user had denied the permission is last launch     
        2A. The user denied permission earlier WITHOUT checking "Never ask again" 
        2B. The user denied permission earlier WITH checking "Never ask again". 
    

따라서, 최하위는 다음과 같습니다 : 우리는 두 가지 경우를 처리 할 필요가

PermissionUtils.java에는 5 개의 추상 메소드가 포함 된 인터페이스가 정의되어 있습니다. 이 메소드들은 위에 논의 된 것처럼 여러 경우에 호출 될 콜백입니다.

마지막으로 Google의 활동에서 우리는 청취자의 콜백을 구현하여 이러한 모든 사례를 처리합니다.

PermissionUtils.checkPermission(MainActivity.this, 
      Manifest.permission.ACCESS_FINE_LOCATION, 
      new PermissionUtils.PermissionAskListener() { 
       @Override 
       public void onPermissionGranted() { 
        updateUI(); 

       } 

       @Override 
       public void onPermissionRequest() { 

        ActivityCompat.requestPermissions(MainActivity.this, 
          new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
          My_PERMISSION_ACCESS_FINE_LOCATION); 


       } 

       @Override 
       public void onPermissionPreviouslyDenied() { 

        //Show an alert message and "request the permission" in its "setPositiveButton" 
        //...and in "setOnNegativeButton", just cancel the dialog and do not run the 
        //...functionality that requires this permission (here, ACCESS_FINE_LOCATION) 
        new AlertDialog.Builder(MainActivity.this) 
          .setTitle("Permission required") 
          .setMessage("Location is required for this application to work ! ") 
          .setPositiveButton("Allow", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            ActivityCompat.requestPermissions(MainActivity.this, 
              new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
              My_PERMISSION_ACCESS_FINE_LOCATION); 


           } 

          }) 
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            dialog.cancel(); 
            finish(); 
           } 
          }) 
          .show(); 


       } 

       @Override 
       public void onPermissionDisabled() { 



        new AlertDialog.Builder(MainActivity.this) 
          .setTitle("Permission Disabled") 
          .setMessage("Please enable the permission in \n Settings>Uber>Permission \n and check 'location' permission") 
          .setPositiveButton("Go to settings", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            startActivity(new Intent(Settings.ACTION_SETTINGS)); 


           } 

          }) 
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            dialog.cancel(); 
            finish(); 
           } 
          }) 
          .show(); 


       } 
      }); 


} 

희망이 있습니다.

+0

+1 : 좋은 답변입니다! 그러나 개선 제안으로, 허가 기준별로 "처음 시작된 응용 프로그램"을 추적하는 것이 유용 할 수 있습니다. 이는 PREFS_FIRST_TIME_KEY + 권한을 기반으로 "처음"키를 생성하여 쉽게 수행 할 수 있습니다. 이렇게하면'getApplicationLaunchedFirstTime' 메소드를 변경하여'permission' 키를 매개 변수로 받아 들일 수도 있습니다. –

+0

여러 권한이 있으면 어떻게됩니까? – Tartar

1

허가를 요청하는 데는 두 가지 단계가 있습니다. 이유는 근거를 제시 한 다음 실제로 사용 권한을 요청하는 것입니다.

그들은 것 순서로 표시된 두 개의 별도의 대화 상자로 일반적으로 매니페스트 :

  1. 단순히 권한 실제로 요청
  2. 대화를 필요로하는 이유 텍스트가 이유 (근거)를 사용자에게 제공이 포함 된 대화 허가. 아래 그림과 같습니다 : image of permissions dialog

문서의 코드가 정확합니다. 논리는 다음과 같습니다

문서 당으로
if we need to show a rationale: 
    show the rationale dialog 
otherwise: 
    just ask for permissions to be granted without showing the rationale dialog 
2

또 다른 옵션은 Google이 자체적으로 제공 한 EasyPermissions을 사용하는 것으로, "Android M 시스템 권한 단순화"입니다. 그렇다면 shouldShowRequestPermissionRationale에 직접 전화 할 필요가 없습니다.

그러면 shouldShowRequestPermissionRationale을 호출하거나 반환 값을 직접 처리 할 필요가 없습니다.