Search code examples
androidkotlinandroid-activityandroid-automotive

How to request permissions in templated apps built for Android Automotive OS?


I am trying to make a simple app in Android (13) Automotive OS. My understanding is quite limited, especially with regards to activities. Within the onGetTemplate, I hope to ask for a permission, which requires an Activity instance. Is it possible to obtain Activity from here?

class AutoDemoScreen(carContext: CarContext) : Screen(carContext) {
    override fun onGetTemplate(): Template { ... }
}

I have tried recursively looking, using getBaseContext(), but no Activity is to be found. Am I supposed to create a new Fragment subclass, and somehow link it to my main screen? I have been looking at some GitHub repositories but it's mostly difficult to parse for me.

Thank you.


Solution

  • You can use CarContext's requestPermissions(...) method to request permissions in an app built using the Car App Library. See Request permissions for details. If you'd prefer to implement your own permission activity, the implementation of that method may be a helpful reference for how to do it yourself.

    I have tried recursively looking, using getBaseContext(), but no Activity is to be found.

    This is to be expected as the screen code is running as part of a service (your CarAppService), not an activity.