前语

注:只想看完结的朋友们能够直接跳到终究面的终究完结

咱们是否还在为动态权限请求感到苦恼呢?传统的动态权限请求需求在Activity中重写onRequestPermissionsResult办法来接收用户权限颁发的成果。试想一下,你需求在一个子模块中请求权限,那得从这个模块地点的ActivityonRequestPermissionsResult中将成果一层层再传回到这个模块中,适当的费事,代码也适当冗余和不干净,逼死强迫症。

运用

为了处理这个痛点,我封装出了两个办法,用于随时随地快速的动态请求权限,咱们先来看看咱们的封装办法是怎么调用的:

activity.requestPermission(Manifest.permission.CAMERA, onPermit = {
    //请求权限成功 Do something
}, onDeny = { shouldShowCustomRequest ->
    //请求权限失利 Do something
    if (shouldShowCustomRequest) {
        //用户挑选了拒绝而且不在问询,此时应该运用自定义弹窗提示用户授权(可选)
    }
})

这样是不是非常的简略便捷?请求和成果回调都在一个办法内处理,而且支撑随用随调。

方案

那么,这么便利好用的办法是怎样完结的呢?不知道小伙伴们在平常开发中有没有留意到过,当你调用startActivityForResult时,AS会提示你该办法已被弃用,点进去看会告知你应该运用registerForActivityResult办法代替。没错,这便是androidx给咱们提供的ActivityResult功用,而且这个功用不只支撑ActivityResult回调,还支撑打开文档,拍照照片,挑选文件等各种各样的回调,同样也包括咱们今日要说的权限请求

其实Android在官方文档 请求运行时权限 中就已经将其作为动态权限请求的引荐办法了,如下示例代码所示:

val requestPermissionLauncher =
    registerForActivityResult(RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            // Permission is granted. Continue the action or workflow in your
            // app.
        } else {
            // Explain to the user that the feature is unavailable because the
            // feature requires a permission that the user has denied. At the
            // same time, respect the user's decision. Don't link to system
            // settings in an effort to convince the user to change their
            // decision.
        }
    }
when {
    ContextCompat.checkSelfPermission(
            CONTEXT,
            Manifest.permission.REQUESTED_PERMISSION
            ) == PackageManager.PERMISSION_GRANTED -> {
        // You can use the API that requires the permission.
    }
    shouldShowRequestPermissionRationale(...) -> {
        // In an educational UI, explain to the user why your app requires this
        // permission for a specific feature to behave as expected, and what
        // features are disabled if it's declined. In this UI, include a
        // "cancel" or "no thanks" button that lets the user continue
        // using your app without granting the permission.
        showInContextUI(...)
    }
    else -> {
        // You can directly ask for the permission.
        // The registered ActivityResultCallback gets the result of this request.
        requestPermissionLauncher.launch(
                Manifest.permission.REQUESTED_PERMISSION)
    }
}

提到这儿,可能有小伙伴要质疑我了:“官方文档里都写明了的东西,你还特别写一遍,还起了这么个标题,是不是在水文章?!”

莫急,假如你遵循以上办法这么写的话,在实际调用的时分会直接产生崩溃:

java.lang.IllegalStateException:
LifecycleOwner Activity is attempting to register while current state is RESUMED. 
LifecycleOwners must call register before they are STARTED.

这段报错很明显的告知咱们,咱们的注册作业有必要要在Activity声明周期STARTED之前进行(也便是onCreate时和onStart完结前),但这样咱们就有必要要事先注册好所有可能会用到的权限,没办法做到随时随地有需求时再请求权限了,有办法处理这个问题吗?答案是必定的。

绕过生命周期检测

想处理这个问题,咱们有必要要知道问题的成因,让咱们带着问题进到源码中一探终究:

public final <I, O> ActivityResultLauncher<I> registerForActivityResult(
        @NonNull ActivityResultContract<I, O> contract,
        @NonNull ActivityResultCallback<O> callback) {
    return registerForActivityResult(contract, mActivityResultRegistry, callback);
}
public final <I, O> ActivityResultLauncher<I> registerForActivityResult(
        @NonNull final ActivityResultContract<I, O> contract,
        @NonNull final ActivityResultRegistry registry,
        @NonNull final ActivityResultCallback<O> callback) {
    return registry.register(
            "activity_rq#" + mNextLocalRequestCode.getAndIncrement(), this, contract, callback);
}
public final <I, O> ActivityResultLauncher<I> register(
        @NonNull final String key,
        @NonNull final LifecycleOwner lifecycleOwner,
        @NonNull final ActivityResultContract<I, O> contract,
        @NonNull final ActivityResultCallback<O> callback) {
    Lifecycle lifecycle = lifecycleOwner.getLifecycle();
    if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
        throw new IllegalStateException("LifecycleOwner " + lifecycleOwner + " is "
                + "attempting to register while current state is "
                + lifecycle.getCurrentState() + ". LifecycleOwners must call register before "
                + "they are STARTED.");
    }
    registerKey(key);
    LifecycleContainer lifecycleContainer = mKeyToLifecycleContainers.get(key);
    if (lifecycleContainer == null) {
        lifecycleContainer = new LifecycleContainer(lifecycle);
    }
    LifecycleEventObserver observer = new LifecycleEventObserver() { ... };
    lifecycleContainer.addObserver(observer);
    mKeyToLifecycleContainers.put(key, lifecycleContainer);
    return new ActivityResultLauncher<I>() { ... };
}

咱们能够发现,registerForActivityResult实际上便是调用了ComponentActivity内部成员变量mActivityResultRegistry.register办法,而在这个办法的一开头就查看了当时Activity的生命周期,假如生命周期坐落STARTED后则直接抛出异常,那咱们该怎么绕过这个限制呢?

其实在register办法的下面就有一个同名重载办法,这个办法并没有做生命周期的检测:

public final <I, O> ActivityResultLauncher<I> register(
        @NonNull final String key,
        @NonNull final ActivityResultContract<I, O> contract,
        @NonNull final ActivityResultCallback<O> callback) {
    registerKey(key);
    mKeyToCallback.put(key, new CallbackAndContract<>(callback, contract));
    if (mParsedPendingResults.containsKey(key)) {
        @SuppressWarnings("unchecked")
        final O parsedPendingResult = (O) mParsedPendingResults.get(key);
        mParsedPendingResults.remove(key);
        callback.onActivityResult(parsedPendingResult);
    }
    final ActivityResult pendingResult = mPendingResults.getParcelable(key);
    if (pendingResult != null) {
        mPendingResults.remove(key);
        callback.onActivityResult(contract.parseResult(
                pendingResult.getResultCode(),
                pendingResult.getData()));
    }
    return new ActivityResultLauncher<I>() { ... };
}

找到这个办法就简略了,咱们将registerForActivityResult办法调用替换成activityResultRegistry.register调用就能够了

当然,咱们还需求留意一些小细节,查看生命周期的register办法同时也会注册生命周期回调,当Activity被销毁时会将咱们注册的ActivityResult回调移除,咱们也需求给咱们封装的办法加上这个逻辑,终究完结就如下所示。

终究完结

private val nextLocalRequestCode = AtomicInteger()
private val nextKey: String
    get() = "activity_rq#${nextLocalRequestCode.getAndIncrement()}"
fun ComponentActivity.requestPermission(
    permission: String,
    onPermit: () -> Unit,
    onDeny: (shouldShowCustomRequest: Boolean) -> Unit
) {
    if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
        onPermit()
        return
    }
    var launcher by Delegates.notNull<ActivityResultLauncher<String>>()
    launcher = activityResultRegistry.register(
        nextKey,
        ActivityResultContracts.RequestPermission()
    ) { result ->
        if (result) {
            onPermit()
        } else {
            onDeny(!ActivityCompat.shouldShowRequestPermissionRationale(this, permission))
        }
        launcher.unregister()
    }
    lifecycle.addObserver(object : LifecycleEventObserver {
        override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
            if (event == Lifecycle.Event.ON_DESTROY) {
                launcher.unregister()
                lifecycle.removeObserver(this)
            }
        }
    })
    launcher.launch(permission)
}
fun ComponentActivity.requestPermissions(
    permissions: Array<String>,
    onPermit: () -> Unit,
    onDeny: (shouldShowCustomRequest: Boolean) -> Unit
) {
    var hasPermissions = true
    for (permission in permissions) {
        if (ContextCompat.checkSelfPermission(
                this,
                permission
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            hasPermissions = false
            break
        }
    }
    if (hasPermissions) {
        onPermit()
        return
    }
    var launcher by Delegates.notNull<ActivityResultLauncher<Array<String>>>()
    launcher = activityResultRegistry.register(
        nextKey,
        ActivityResultContracts.RequestMultiplePermissions()
    ) { result ->
        var allAllow = true
        for (allow in result.values) {
            if (!allow) {
                allAllow = false
                break
            }
        }
        if (allAllow) {
            onPermit()
        } else {
            var shouldShowCustomRequest = false
            for (permission in permissions) {
                if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                    shouldShowCustomRequest = true
                    break
                }
            }
            onDeny(shouldShowCustomRequest)
        }
        launcher.unregister()
    }
    lifecycle.addObserver(object : LifecycleEventObserver {
        override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
            if (event == Lifecycle.Event.ON_DESTROY) {
                launcher.unregister()
                lifecycle.removeObserver(this)
            }
        }
    })
    launcher.launch(permissions)
}

总结

其实许多实用技巧本质上都是很简略的,但没有接触过就很难想到,我将我的开发经历分享给咱们,希望能协助到咱们。