質問で説明されている理由から、利用可能なライブラリを使用したくなかったので、自分で何かを開発しました。
1 つ以上のアクセス許可を必要とするすべてのアクティビティはPermissionActivity
、アクセス許可に関連するすべてのタスクを処理する から継承します。
それがどのように機能するかは、あなたのアクティビティコールです
if (checkHasPermission(Manifest.permission.ACCESS_FINE_LOCATION)) { }
親クラスから。アクセス許可が既に付与されている場合は、コードを続行できます。そうでない場合、親クラスはパーミッションを要求し、抽象メソッドおよび/または 1 つまたは複数のオーバーライド可能なメソッドを使用して結果を子クラスに送信します。
親クラス
messageForRationale()
このクラスは、およびの switch ブロックを除いて、変更しないでおくことができますrequestCodeForPermission()
。アプリに必要なアクセス許可についてそれらを更新します。
/**
* An activity that can be extended to simplify handling permissions.
* <p>
* Deriving classes will not have to write boilerplate code and code duplication between activities
* that share this functionality is avoided.
*/
public abstract class PermissionActivity extends AppCompatActivity {
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// If multiple permissions were requested in one call, check if they were all granted.
if (requestCode == RequestCode.PERMISSION_MULTIPLE) {
boolean allPermissionsGranted = true;
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
allPermissionsGranted = false;
}
}
if (allPermissionsGranted) {
onAllPermissionsGranted(permissions);
return;
}
}
// Else, check each one if it was granted/denied/blocked.
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
// User granted permission.
onPermissionGranted(permissions[i]);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
// User denied permission.
onPermissionDenied(permissions[i]);
} else {
// User denied permission and checked 'never ask again'.
onPermissionBlocked(permissions[i]);
}
}
}
}
/**
* Checks if the app has the given permission(s).
* <p>
* If not, it will request them.
* <p>
* The method is called `checkHasPermission` to avoid the linter showing a warning in the
* child class when it's delegating permission checks to its parent class. See
* http://stackoverflow.com/questions/36031218/check-android-permissions-in-a
* -method/36193309#36193309 for details.
*/
public boolean checkHasPermission(int requestCode, String... permissions) {
if (!(permissions.length > 0)) {
throw new IllegalArgumentException("must request at least one permission");
}
if (requestCode == RequestCode.PERMISSION_MULTIPLE) {
List<String> permissions_ = new ArrayList<>();
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
permissions_.add(permission);
}
}
if (!permissions_.isEmpty()) {
requestPermissions(this, permissions_.toArray(new String[permissions_.size()]), requestCode);
return false;
} else {
return true;
}
} else {
if (ActivityCompat.checkSelfPermission(this, permissions[0]) !=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(this, permissions, requestCode);
return false;
} else {
return true;
}
}
}
/**
* Requests the given permissions.
*/
private void requestPermissions(Activity activity, String permissions[], int resultCode) {
showRequestPermissionsDialog(activity, permissions, resultCode);
}
/**
* Called when a rationale (explanation why a permission is needed) should be shown to the user.
* <p>
* If the user clicks the positive button, the permission is requested again, otherwise the
* dialog is dismissed.
*/
public void showRationaleDialog(Activity activity, String permission, String message,
int resultCode) {
new AlertDialog.Builder(activity)
.setMessage(message)
.setPositiveButton("ok", (dialog, which) ->
showRequestPermissionDialog(activity, permission, resultCode))
.setNegativeButton("not now", (dialog, which) -> { /* Do nothing */ })
.show();
}
/**
* Requests a single permission.
*/
private void showRequestPermissionDialog(Activity activity, String permission, int resultCode) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, resultCode);
}
/**
* Requests multiple permissions in one call.
*/
private void showRequestPermissionsDialog(Activity activity, String[] permissions,
int resultCode) {
ActivityCompat.requestPermissions(activity, permissions, resultCode);
}
/**
* Returns a message to be shown to the user that explains why a specific permission is
* required.
*/
public String messageForRationale(String permission) {
String s;
switch (permission) {
case Manifest.permission.READ_PHONE_STATE:
s = "access this device's state";
break;
case Manifest.permission.ACCESS_FINE_LOCATION:
s = "access the location of this device";
break;
case Manifest.permission.SEND_SMS:
s = "send text messages";
break;
default:
throw new IllegalArgumentException("Permission not handled: " + permission);
}
return String.format("MyApp needs permission to %s.", s);
}
/**
* Get the RequestCode for the given permission.
*/
public int requestCodeForPermission(String permission) {
int code;
switch (permission) {
case Manifest.permission.READ_PHONE_STATE:
code = RequestCode.PERMISSION_READ_PHONE_STATE;
break;
case Manifest.permission.ACCESS_FINE_LOCATION:
code = RequestCode.PERMISSION_FINE_LOCATION;
break;
case Manifest.permission.SEND_SMS:
code = RequestCode.PERMISSION_SEND_SMS;
break;
// TODO: add required permissions for your app
default:
throw new IllegalArgumentException("Permission not handled: " + permission);
}
return code;
}
/**
* Called if all requested permissions were granted in the same dialog.
* E.g. FINE_LOCATION and SEND_SMS were requested, and both were granted.
* <p>
* Child class can override this method if it wants to know when this happens.
* <p>
* Linter can show an unjust "call requires permission" warning in child class if a method that
* requires permission(s) is called. Silence it with `@SuppressWarnings("MissingPermission")`.
*/
protected void onAllPermissionsGranted(String[] permissions) {
}
/**
* Called for all permissions that were granted in the same dialog, in case not all were
* granted. E.g. if FINE_LOCATION, COARSE_LOCATION and SEND_SMS were requested and FINE_LOCATION
* was not granted but COARSE_LOCATION and SEND_SMS were, it will be called for COARSE_LOCATION
* and SEND_SMS.
* <p>
* Child class can override this method if it wants to know when this happens.
* <p>
* Linter can show an unjust "call requires permission" warning in child class if a method that
* requires permission(s) is called. Silence it with `@SuppressWarnings("MissingPermission")`.
*/
protected void onPermissionGranted(String permission) {
}
/**
* Called for all permissions that were denied in the same dialog, handled one by one.
* <p>
* Child class should not override this general behavior.
*/
protected void onPermissionDenied(String permission) {
String message = messageForRationale(permission);
showRationaleDialog(this, permission, message, requestCodeForPermission(permission));
}
/**
* Called for all permissions that were blocked in the same dialog, handled one by one.
* <p>
* Blocked means a user denied a permission with the 'never ask again' checkbox checked.
* <p>
* Child class must override and decide what to do when a permission is blocked.
*/
protected abstract void onPermissionBlocked(String permission);
}
->
表記はラムダ式です。
RequestCodeは、数値を抽象化するためだけに使用されるインターフェースです。
public interface RequestCode {
int PERMISSION_READ_PHONE_STATE = 0;
int PERMISSION_FINE_LOCATION = 1;
int PERMISSION_SEND_SMS = 2;
int PERMISSION_MULTIPLE = 3;
}
好きなように変更できます。256 を超える数値は使用しないでください。使用すると、次のような例外がスローされます。
リクエストコードは下位8ビットのみ使用できます。
許可が必要な活動では、このように使用できます(一例)。必ずアクティビティをextend PermissionActivity
private void callThisSomewhere() {
if (checkHasPermission(RequestCode.PERMISSION_READ_PHONE_STATE,
Manifest.permission.READ_PHONE_STATE)) {
tryDoStuffWithPhoneState();
}
}
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
private void doStuffWithPhoneState() {
// Do stuff.
}
@Override
public void onPermissionGranted(String permission) {
tryDoStuffWithPhoneState();
}
@Override
public void onPermissionBlocked(String permission) {
// Disable parts of app that require this permission.
}
一度に複数のアクセス許可を要求する場合は、RequestCode.PERMISSION_MULTIPLE
. それ以外の場合は、最初の許可のみが要求されます。
AndroidManifest.xml にリストされていないアクセス許可は、ユーザーにダイアログを表示せずに自動的にブロックされるため、マニフェストにも要求する必要があるアクセス許可を必ず追加してください。
制限事項
このソリューションは、フラグメントまたはサービスと互換性がありません。