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/ ) の資料

D8281434c0409ba2051cd3f7590e4c2f?s=128

Hiroyuki Kusu

November 27, 2020
Tweet

Transcript

  1. Runtime permissions in Android 11 2020.11.27 YUMEMI.apk #2 Hiroyuki Kusu

    ( @hkusu_ )
  2. About me

  3. • 1ճ͚ͩͷΞΫηεڐՄΦϓγϣϯ͕௥ՃʢҐஔ৘ใɺϚΠΫɺΧϝϥͷ৔߹ͷΈʣ • ෳ਺ճڋ൱͢Δͱʮࠓޙ͸֬ೝ͠ͳ͍ʯঢ়ଶͱͳΔ • ݖݶΛࣗಈతʹϦηοτ͢Δઃఆ͕௥Ճ ΞϓϦͷ targetSdkVersion ʹؔ܎ͳ͘ɺAPI Level

    30 Ҏ্ͷσόΠεͰ
  4. Ґஔ৘ใͷ৔߹

  5. ※ ΞϓϦͷ targetSdkVersion ͕ 30Ҏ্ͷ৔߹͸σϑΥϧτͰࣗಈϦηοτ͕ON

  6. ैདྷͲ͓ΓɺػೳΛར༻͢Δ௚લͰ permission Λ औಘ͢ΔίʔυΛॻ͍͍ͯΕ͹ɺͱΓ͋͑ͣ͸େৎ෉ͦ͏ɻ

  7. 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 Ҏ߱Ͱར༻͕Մ ϘλϯΛԡԼͨ͠ΒҐஔ৘ใΛར༻ͨ͠ԿΒ͔ͷॲཧΛ࣮ߦ͢Δྫɿ
  8. • ContextCompat.checkSelfPermission() • ݱ࣌఺Ͱݖݶ͕ڐՄ͞Ε͍ͯΔ͔ • ActivityCompat.shouldShowRequestPermissionRationale() • ʮࠓޙ͸֬ೝ͠ͳ͍ʯঢ়ଶ͔ • PackageManager#isAutoRevokeWhitelisted()

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