DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android 4.2: How to Create DayDream?

Android 4.2: How to Create DayDream?

Paresh  Mayani user avatar by
Paresh Mayani
·
Dec. 14, 12 · Interview
Like (0)
Save
Tweet
Share
8.96K Views

Join the DZone community and get the full member experience.

Join For Free

 

This article is extension of my previous article about DayDream: What exactly DayDream feature is about?. Now i assume you understood about DayDream, so now i am proceeding further for developing a small example of DayDream.

Step 1: Create a sample project to build this demo example

I have created a simple project to give this demo, you can follow below steps to create DayDream for your Android application.

Step 2: Create a class and extends DreamService

FYI, DreamService is added in API Level 17 and you can it extend to create custom DayDream. LifeCycle of the DreamService 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 your dream set up. For example, detach from handlers and listeners.
Lifecyle of DreamService

Here in this example i have just included TextView to display Hello World message, instead you can create your own XML layout and set it through setContentView() method inside onAttachedWindow() method.

package com.technotalkative.daydreamexample;
 
import android.graphics.Color;
import android.service.dreams.DreamService;
import android.widget.TextView;
 
public class MyDreamService extends DreamService {
    @Override
   public void onAttachedToWindow() {
       super.onAttachedToWindow();
 
       // Allow user touch
       setInteractive(true);
 
       // Hide system UI
       setFullscreen(true);
 
       // Set the dream layout
       TextView txtView = new TextView(this);
       setContentView(txtView);
       txtView.setText("Hello DayDream world from TechnoTalkative.com !!");
       txtView.setTextColor(Color.rgb(184, 245, 0));
       txtView.setTextSize(30);
 
   }
}

Step 3: Declare this MyDreamService service in AndroidManifest file

This step is necessary to make our DayDream available to the system.

<service
     android:name=".MyDreamService"
     android:exported="true"
     android:label="TechnoTalkative - DayDream" >
     <intent-filter>
        <action android:name="android.service.dreams.DreamService" />
        <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
 </service>

Step 4: Run this application

TechnoTalkative DayDream Example

Now, lets run this application, if it executes well then your DayDream will become available inside the Settings->Display->DayDream. Yes as we did nothing for Activity screen we will see nothing in app screen.

You can see TechnoTalkative – DayDream is available inside DayDream settings. Now select it and press START NOW it would be saying Hello DayDream world from TechnoTalkative.com :)

Output:

TechnoTalkative - DayDream example output

Download source code of this example: DayDream Example – 1


Android (robot)

Published at DZone with permission of Paresh Mayani. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • OpenID Connect Flows
  • How Do the Docker Client and Docker Servers Work?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: