6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ActivityResultContracts.RequestMultiplePermissions チートシート

Last updated at Posted at 2021-03-20

最近はパーミッション関連の処理が随分と簡潔に書けるようになったのは嬉しいのだけれども、なにが困るってJavaで書かなければならない時に少し混乱することがあるのでチートシートとして :scroll:

Java

private final ActivityResultLauncher<String[]> requestPermissionsLauncher =
        registerForActivityResult(
                new ActivityResultContracts.RequestMultiplePermissions(),
                (Map<String, Boolean> grantStates) -> {
                    for (Map.Entry<String, Boolean> grantState : grantStates.entrySet()) {
                        Timber.d(grantState.getKey() + " - " + grantState.getValue());
                    }
                });

private void requestPermissions() {
    requestPermissionsLauncher.launch(new String[]{
            Manifest.permission.Foo,
            Manifest.permission.Bar
    });
} 

Kotlin

private val requestPermissionsLauncher =
        registerForActivityResult(RequestMultiplePermissions()) { grantStates: Map<String, Boolean> ->
            for ((permission, granted) in grantStates) {
                Timber.d("$permission - $granted")
            }
        }

private fun requestPermissions() {
    requestPermissionsLauncher.launch(arrayOf(
            Manifest.permission.Foo,
            Manifest.permission.Bar
    ))
}
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?