Search code examples
androidservicemanifestdreamservicedaydream

Conditionally declare service in AndroidManifest


I have a service, in particular an Android Daydream DreamService, that I have declared in my AndroidManifest.xml file. When my app is loaded, Android detects this declaration and adds my app as an option to the list of Daydreams the user can choose from in their Settings. I am declaring this Service as follows...

<service android:name=".daydream.DaydreamService" android:exported="true"
        android:icon="@drawable/icon" android:label="@string/app_name" >
   <intent-filter>
        <action android:name="android.service.dreams.DreamService" />
        <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>

   <meta-data
        android:name="android.service.dream"
        android:resource="@xml/daydream" />

</service>

I'm trying to see if there is a way I can conditionally load this Service declaration programmatically. In other words, I want to only declare this DreamService based on some programmatic logic. I don't want Android to list or even be aware of my app's Daydream feature unless I programmatically enable it.

Is something like this possible?


Solution

  • Is something like this possible?

    Yes, it is, even without the boldface. :-)

    Call setComponentEnabledSetting() on PackageManager to toggle whether or not the component is enabled. Use the android:enabled attribute in the manifest to indicate its starting state, where the overall default is that it is enabled.

    So, you could add android:enabled="false" on your .daydream.DaydreamService component to have it be initially disabled, then use setComponentEnabledSetting() to enable it later on as needed.