user3113441 user3113441 - 20 days ago 11
Android Question

In android, how to change the settings of Daydream from code?

I want to write a application which can change the daydream setting. It will require to set the my own dream as selected and also make the when to play option as "Either". Is that possible to implement this feature in the sdk version 19?

Answer

If you want to set the daydream for the user, you cannot do this. You can, however, open the system settings in the right location so that the user can select from installed daydreams.

You can provide a button to access Daydream Settings like so:

public void onSettingsButtonClick(View v) {
    Intent intent;
    if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
        intent = new Intent(Settings.ACTION_DREAM_SETTINGS);
    } else {
        intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS);
    }
    startActivity(intent);
}

This will take the user to either "Daydream Settings" or the "Display Settings" section of the device settings.

If you'd like the user to be able to go from the device settings to a specific activity for configuring your daydream, you can add the <meta-data/> tag here as an element of your Daydream service in your manifest:

    <service
        android:name="some.package.SomeDaydream"
        android:exported="true"
        android:icon="@drawable/ic_launcher"
        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/dream_info" />
    </service>

When targeting api level 21 and above, you must declare the service in your manifest file with the BIND_DREAM_SERVICE permission. For example:

 android:permission="android.permission.BIND_DREAM_SERVICE">

Then, in /res/xml/, add dream_info.xml:

<?xml version="1.0" encoding="utf-8"?>
<dream xmlns:android="http://schemas.android.com/apk/res/android"
    android:settingsActivity="some.package/.SomeActivity" />

I have an example Daydream here that shows this behaviour (in both directions).