,
shouldShowRequestPermissionRationale
반환 우리가 허가를 요청하기위한 근거로 UI를 표시해야합니다> 여부를 나타내는 부울. 염두에두고
Allow SnazzyApp to access your contacts ? //this is NOT our custom UI
, 지금
:
이 UI는 우리의 장치 쇼 (아래 참조), NOT 대화 상자를 (우리가 예를 들어,에 AlertDialog를 표시 할 수 있습니다) 우리의 사용자 정의 UI입니다
shouldShowRequestPermissionRationale
의 반환 값은 순서도에 표시된 것과 같습니다.또한
점에 유의
, 따라서
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();
}
}
}
}
논리
- 우리는 먼저 우리가 심지어 처음에 실행 권한이 필요합니까 선택하여 시작
? 우리는 권한이없는 경우 우리가
ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED)
이미 이전에 해당 권한을 가지고 있었다이라면, 우리는 실행 권한을 필요로 할 경우
if (!isRuntimePermissionRequired()) {...}
, 우리가 확인 :이은에 의해 이루어집니다
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();
}
});
}
희망이 있습니다.
+1 : 좋은 답변입니다! 그러나 개선 제안으로, 허가 기준별로 "처음 시작된 응용 프로그램"을 추적하는 것이 유용 할 수 있습니다. 이는 PREFS_FIRST_TIME_KEY + 권한을 기반으로 "처음"키를 생성하여 쉽게 수행 할 수 있습니다. 이렇게하면'getApplicationLaunchedFirstTime' 메소드를 변경하여'permission' 키를 매개 변수로 받아 들일 수도 있습니다. –
여러 권한이 있으면 어떻게됩니까? – Tartar