This tutorial is all about creating your own Dream by Implementing DremService class. You can create custom DayDream by implementing this class in your application. DreamService is added in API Level 17 and you can extend it 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 write messages 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.
Follow the steps below and you will get your own DayDream for your android application in minutes. 😉
Step 1:
Create a sample project to build this demo example
Step 2:
Create a classCustomDreamService and extends DreamService:
package com.example.daydreampart2; import android.graphics.Color; import android.service.dreams.DreamService; import android.widget.TextView; public class CustomDreamService extends DreamService { @Override public void onAttachedToWindow() { super.onAttachedToWindow(); setInteractive(true); setFullscreen(true); TextView tv = new TextView(this); setContentView(tv); tv.setText("This is your own Dream Hurray !!"); tv.setTextColor(Color.rgb(184, 245, 0)); tv.setTextSize(30); } }
Step 3:
Write the following into MainActivity.java:
package com.example.daydreampart2; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } }
Step 4:
Declare CustomDreamService service in AndroidManifest file
<service android:name=".CustomDreamService" android:exported="true" android:label="Custom DayDream" > <intent-filter> <action android:name="android.service.dreams.DreamService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
Step 5:
Run this application
Now, lets run this application, if it executes well then your DayDream will become available inside the Settings->Display->DayDream. As we did nothing for Activity screen we will see nothing in app screen.
You can see CustomDayDream is available in DayDream settings. Now select it and press START NOW it would be saying. This is your own Dream Hurray!!
Output:
Read my previous blog on details on What is DayDream and Introduction.
Top 10 Android App Development Trends | 2020 Guide
5 Best Resources to Get Started with Android Nougat
Android Studio Introduction
Services – An Android Component
Applying MediaCodec On An Open Source Android Audio Player
5 Most Used Android Testing Frameworks
Android Language Highlights A Developers Perspective
Android KitKat Development