Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Runtime permissions in Android 11

Runtime permissions in Android 11

Yumemi.apk #2 ( https://yumemi.connpass.com/event/191284/ ) の資料

Hiroyuki Kusu

November 27, 2020
Tweet

More Decks by Hiroyuki Kusu

Other Decks in Programming

Transcript

  1. class MainFragment : Fragment(R.layout.main_fragment) { // ... override fun onViewCreated(view:

    View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // Fragmentͷੜ੒ϑΣʔζͰొ࿥͠ͳ͍ͱΤϥʔʹͳΔͷͰ஫ҙ val locationPermission = registerForActivityResult( ActivityResultContracts.RequestPermission() ) { isGranted -> when { // طʹڐՄ͞Ε͍ͯͨ or μΠΞϩά͕දࣔ͞Ε[ΞϓϦͷ࢖༻࣌ͷΈ]·ͨ͸[ࠓճͷΈ]͕ԡԼ͞Εͨ isGranted -> { // ... } // طʹڋ൱͞Ε͍ͯͨ or μΠΞϩά͕දࣔ͞Ε[ڐՄ͠ͳ͍]͕ԡԼ͞Εͨ else -> { // ... } } } button.setOnClickListener { locationPermission.launch(Manifest.permission.ACCESS_FINE_LOCATION) } ※ ActivityResultContracts ͸ androidx.activity ͷ 1.2.0ɺandroidx.fragment ͷ 1.3.0 Ҏ߱Ͱར༻͕Մ ϘλϯΛԡԼͨ͠ΒҐஔ৘ใΛར༻ͨ͠ԿΒ͔ͷॲཧΛ࣮ߦ͢Δྫɿ
  2. • ContextCompat.checkSelfPermission() • ݱ࣌఺Ͱݖݶ͕ڐՄ͞Ε͍ͯΔ͔ • ActivityCompat.shouldShowRequestPermissionRationale() • ʮࠓޙ͸֬ೝ͠ͳ͍ʯঢ়ଶ͔ • PackageManager#isAutoRevokeWhitelisted()

    • ݖݶͷࣗಈϦηοτ͕ONͰ͋Δ͔ • ͨͩ͠৽͍͠APIͳͷͰҎԼͷ৔߹ͷΈར༻͕Մೳ • compileSdkVersion ͕ 30Ҏ্ • API Level 30Ҏ্ͷσόΠε • Build.VERSION.SDK_INT • σόΠεͷ API Level • 30ະຬ͸ैདྷͷpermissionͷ࢓༷ͷ··ͳͷͰUIΛ෼͚Δ৔߹ ͨͩ͠ɺϢʔβʹڐՄΛଅ͢ͳΒɺ ͋ͨΓΛར༻ͯ͠ಋઢ΍จݴΛ੍ޚ͠ɺUIΛ޻෉ͨ͠ํ͕Αͦ͞͏ɻ