Creating Interactive Screen Saver with Daydream

DayDreams are interactive screensavers launched when a charging device is idle, or docked in a desk dock. Dreams provide another modality for apps to express themselves, tailored for an exhibition/lean-back experience. So it’s found here and an official example can be found here. DayDream feature was introduced in Android 4.2,brings this kind of laid-back, whimsical experience to Android phones and tablets that would otherwise be sleeping.However if you haven’t checked it out, you can turn it on in the Settings app, in Display > Daydream touch When to Daydream to enable the feature when charging or docking or both in some cases.
An attract mode for apps
Apps that support Daydream can also take advantage of the full Android UI toolkit in this mode, which means it’s easy to take existing components of your app — including layouts, animations, 3D, and custom views—and remix them for a more ambient presentation.
And since you can use touchscreen input in this mode as well, you can provide a richly interactive experience if you choose.
.Daydream also provides an opportunity for your app to show off a little bit. However you can choose to hide some of your app’s complexity in favor of one or more visually compelling experiences that can entertain from across a room, possibly drawing the user into your full app, like a video game’s attract mode.
The architecture of a Daydream
In fact each Daydream implementation is a subclass of android.service.dreams.DreamService
. When you extend DreamService
, you’ll have access to a simple Activity-like life-cycle API.
As a matter of fact to be available to the system, your DreamService
should be declared in the manifest as follows:
<service android:name=".DayDream" android:exported="true" android:label="@string/my_daydream_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>
Daydream is only available on devices running version 4.2 of Android , which is API Level 17 and above.When targeting api level 21 and above, you must declare the service in your manifest file with the BIND_DREAM_SERVICE
permission.
In fact additional information is specified with the <
meta-data> element. So additional information for the dream is defined using the <
dream> element in a separate XML file. As a result it allows you to point to an XML resource that specifies a settings Activity specific to your Daydream. Currently, the only addtional information you can provide is for a settings activity that allows the user to configure the dream behavior For example:
res/xml/dream_info.xml
<dream xmlns:android="http://schemas.android.com/apk/res/android" android:settingsActivity="com.logicchip.blog_15_daydream.MainActivity" />
This makes a Settings button available alongside your dream’s listing in the system settings, which when pressed opens the specified activity.
In order to specify your dream layout, call setContentView(View)
, typically during the onAttachedToWindow()
callback For example:
DayDream.java
public class DayDream extends DreamService { Handler handler; Runnable runnable; SimpleDateFormat simpleDateFormat; Calendar calander; @Override public void onAttachedToWindow() { super.onAttachedToWindow(); // Hide system UI setFullscreen(true); // Set the dream layout setContentView(R.layout.dream); } @Override public void onDreamingStarted() { super.onDreamingStarted(); TimeChange12(); } public void TimeChange12(){ handler = new Handler(); runnable = new Runnable() { @Override public void run() { handler.postDelayed(this, 1000); try { calander = Calendar.getInstance(); simpleDateFormat = new SimpleDateFormat("hh : mm : ss"); TextView txtTime= (TextView)findViewById(R.id.txtTime); txtTime.setText(simpleDateFormat.format(calander.getTime())); } catch (Exception e) { e.printStackTrace(); } } }; handler.postDelayed(runnable, 0); } }
Consequently layout for dreaming can be whatever we like, can include animation interactive ui etc.However, be aware that if your screen saver is using too much of the available processing resources, the Android system will stop it from running to allow the device to charge properly.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"> <LinearLayout android:gravity="center" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/logicchip"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="logicchip.com"/> <TextView android:text="23 : 59 : 59" android:textSize="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txtTime"/> </LinearLayout> </RelativeLayout>
Even so “Creating Interactive Screen Saver with Daydream” is explained above you can still download it from here, You can modify the code us u like. In like manner feel free to comment your doubt’s below. Does our tutorials help you then help as spread the word, like and share.