Thanks to contributor talklittle, you can now write Daydream apps with libgdx. Daydream apps are a new feature in Android 4.2. When idle or docked, a 4.2 device can display information like a photo album and so on.
Daydreams are pretty easy to create. Here’s the one from our test collection:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.badlogic.gdx.tests.android; import android.annotation.TargetApi; import android.util.Log; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; import com.badlogic.gdx.backends.android.AndroidDaydream; import com.badlogic.gdx.tests.MeshShaderTest; @TargetApi(17) public class Daydream extends AndroidDaydream { @Override public void onAttachedToWindow() { super.onAttachedToWindow(); setInteractive(false); AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration(); cfg.useGL20 = true; ApplicationListener app = new MeshShaderTest(); initialize(app, cfg); } } |
Extend from AndroidDaydream, then override the onAttachedToWindow method where you create your ApplicationListener and register it via the initialize method. Note that you have to call the super onAttachedToWindow method, just as you have to call onCreate() for a normal AndroidApplication.
You can have a separate standard Android activity that lets a user define specific settings. If you do that you also need to add an xml file in your Android project’s res/xml/ folder. Here’s an example daydream.xml:
1 2 |
<dream xmlns:android="http://schemas.android.com/apk/res/android" android:settingsActivity="com.badlogic.gdx.tests.android/.DaydreamSettings" /> |
In addition to the AndroidDaydream and the settings activity, you have to register a specific service so your daydream is available to the user. Here’s an example snippet from the AndroidManifest.xml file:
1 2 3 4 5 6 7 8 9 10 11 |
<service android:name=".Daydream" android:label="@string/app_name" android:icon="@drawable/icon" android:exported="true"> <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> |
And that’s it.
Nice summary of Daydream apps. Thanks.
Ah great even more waste of battery but its good to have this choice 🙂
It’s worth pointing out that you will also need
android:permission=”android.permission.BIND_DREAM_SERVICE”
as part of your service definition