Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Kotlin for Android 14

You're reading from  Mastering Kotlin for Android 14

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781837631711
Pages 370 pages
Edition 1st Edition
Languages
Author (1):
Harun Wangereka Harun Wangereka
Profile icon Harun Wangereka

Table of Contents (22) Chapters

Preface 1. Part 1: Building Your App
2. Chapter 1: Get Started with Kotlin Android Development 3. Chapter 2: Creating Your First Android App 4. Chapter 3: Jetpack Compose Layout Basics 5. Chapter 4: Design with Material Design 3 6. Part 2: Using Advanced Features
7. Chapter 5: Architect Your App 8. Chapter 6: Network Calls with Kotlin Coroutines 9. Chapter 7: Navigating within Your App 10. Chapter 8: Persisting Data Locally and Doing Background Work 11. Chapter 9: Runtime Permissions 12. Part 3: Code Analysis and Tests
13. Chapter 10: Debugging Your App 14. Chapter 11: Enhancing Code Quality 15. Chapter 12: Testing Your App 16. Part 4: Publishing Your App
17. Chapter 13: Publishing Your App 18. Chapter 14: Continuous Integration and Continuous Deployment 19. Chapter 15: Improving Your App 20. Index 21. Other Books You May Enjoy

Runtime Permissions

As we build our Android apps, there are some functionalities that require permissions to be granted for them to function properly. Due to privacy and data security policies, we as developers can not automatically grant permissions to the apps that we develop. We need to inform the users of the permissions that the apps need and why they need them.

In this chapter, we will understand runtime permissions and how to request them in our app.

In this chapter, we’re going to cover the following main topics:

  • Understanding runtime permissions
  • Requesting permissions at runtime

Technical requirements

To follow the instructions in this chapter, you will need to have Android Studio Hedgehog or later (https://developer.android.com/studio) downloaded.

You can use the previous chapter’s code to follow the instructions in this chapter. You can find the code for this chapter at https://github.com/PacktPublishing/Mastering-Kotlin-for-Android/tree/main/chapternine.

Understanding runtime permissions

Runtime permissions are permissions that are requested at runtime by an app. They are also called dangerous permissions. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request. Android provides several methods you can use to request permission, such as requestPermissions() and checkSelfPermission(). The user only needs to grant permission once during the lifetime of the app.

Some of the features that need permission to be granted to work are camera, location, microphone and storage. Before using them, ensure that a user has permission to use them. If the user has not granted permission, you must request it from them. If the user has denied the permission, you must show a dialog explaining why you need it and ask the user to grant it from the settings. If the user has granted permission, you can use the feature. Failing to do these checks often results in...

Requesting permissions at runtime

We will follow the steps covered in Figure 9.1 to request runtime permissions for our app:

  1. Let us start by adding the permission to the manifest file. We will request permission to access the user’s location. To do this, we add the following permission to the manifest file:
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    This specifies that our app will be using the ACCESS_COARSE_LOCATION permission. Declaring permissions in the manifests is crucial for enhancing security, user awareness, and overall app compatibility. By explicitly specifying the actions or resources apps require access to permissions informs users during installations, allowing them to make informed decisions about granting or denying access. This declaration ensures compatibility across different Android versions and devices, facilitates inter-app communication, and supports intent filtering to control component access. Permissions...

Summary

In this chapter, we explored what runtime permissions are and why we should declare and request permissions in our apps. Step by step, we learned how to request runtime permissions in our app and how to show permission rationale dialogs, explaining to users why we need access to runtime permissions in cases where they have denied apps access to permissions.

In the next chapter, we will learn debugging tips and tricks, how to detect leaks using LeakCanary, how to inspect HTTPS requests/responses fired by our app using Chucker, and how to inspect the Room database.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mastering Kotlin for Android 14
Published in: Apr 2024 Publisher: Packt ISBN-13: 9781837631711
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}