Wednesday 24 December 2014

HB Blog 46: Android Daydream Interactive Screensaver Mode For Android Devices.

Daydream is a new interactive screensaver mode for Android devices. It activates automatically when the device is inserted into a dock or when the device is left idle while plugged in to a charger (instead of turning the screen off). Daydream displays one dream at a time, which may be a purely visual, passive display that dismisses upon touch, or may be interactive and responsive to the full suite of input events. Your dreams run in your app’s process and have full access to the Android UI toolkit, including views, layouts, and animations, so they are more flexible and powerful than either live wallpapers or app widgets.

The DreamService lifecycle is as follows:
    onAttachedToWindow()
    Use this for initial setup, such as calling setContentView().
    onDreamingStarted()
    Your dream has started, so you should begin animations or other behaviors here.
    onDreamingStopped()
    Use this to stop the things you started in onDreamingStarted().
    onDetachedFromWindow()
    Use this to dismantle resources (for example, detach from handlers and listeners).

Refer the below link for complete sample code:-
Download Sample Code
Download Apk File
Have a look on few code snippets,

MyDayDreamService.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class MyDayDreamService extends DreamService {
    @Override
   public void onAttachedToWindow() {
       super.onAttachedToWindow();

       //allow user touch
       setInteractive(true);
 
       // Allow full screen
       setFullscreen(true);
 
       //Add text and image to dream layout
       RelativeLayout r1 = new RelativeLayout(this);
      
       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
       params.addRule(RelativeLayout.CENTER_HORIZONTAL);
      
       TextView textView = new TextView(this);
       textView.setId(1);
       textView.setText("This is a HB demo of the DayDream.");
       textView.setTextSize(35);
       textView.setLayoutParams(params);
       
       r1.addView(textView,params);

       RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(300, 250);
       params2.addRule(RelativeLayout.BELOW,1); 
       params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
       ImageView iv = new ImageView(this);
       iv.setImageResource(R.drawable.android);
       iv.setLayoutParams(params2);
       
       
       r1.addView(iv,params2);
      
       setContentView(r1);
          
   }
}

 AndroidManifest.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <service
        android:name=".MyDayDreamService"
         android:exported="true"
          android:label="AndroidDayDreamDemo" >
        <intent-filter>
          <action android:name="android.service.dreams.DreamService" />
           <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>
   </service>
        
    </application>

</manifest>

No comments:

Post a Comment