天天看點

Android - Daydream 互動屏保Android Daydream 互動屏保

Android Daydream 互動屏保

API19 API23

Create:2016-03-01

繼承DreamService來實作一個自定義屏保

Dreams是當充電的裝置空閑,或者插入底座時顯示的互動屏保。在展覽或陳列時,Dreams為APP提供一個定制的展示方式。

DreamService的生命周期

1.onAttachedToWindow()

初始化設定,在這裡可以調用 setContentView()

2.onDreamingStarted()

互動屏保已經啟動,這裡可以開始播放動畫或者其他操作

3.onDreamingStopped()

在停止 onDreamingStarted() 裡啟動的東西

4.onDetachedFromWindow()

在這裡回收前面調用的資源(比如 handlers 和 listeners)

另外,onCreate 和 onDestroy 也會被調用。但要複寫上面的幾個方法來執行初始化和銷毀操作。

manifest 聲明

為了能讓系統調用,你的 DreamService 應該在 APP 的 manifest 中注冊:

<service
     android:name=".MyDream"
     android:exported="true"
     android:icon="@drawable/my_icon"
     android:label="@string/my_dream_label" >
     <intent-filter>
         <action android:name="android.service.dreams.DreamService" />
         <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
     <!-- Point to additional information for this dream (optional) -->
     <meta-data
         android:name="android.service.dream"
         android:resource="@xml/my_dream" />
 </service>           

如果填寫了

<meta-data>

元素,dream的附加資訊就被指定在XML檔案的

<dream>

元素中。

通常提供的附加資訊是對互動屏保的自定義設定,指向一個自己寫的Activity。

比如:

res/xml/my_dream.xml

<dream xmlns:android="http://schemas.android.com/apk/res/android"
     android:settingsActivity="com.example.app/.MyDreamSettingsActivity" />           

這樣在Settings-Display-Daydream-你的Daydream選項右邊會出現一個設定圖示。點選此圖示可打開指定的activity。

當目标api>=21,必須在manifest中申請

BIND_DREAM_SERVICE

權限,比如:

<service
     android:name=".MyDream"
     android:exported="true"
     android:icon="@drawable/my_icon"
     android:label="@string/my_dream_label"
     android:permission="android.permission.BIND_DREAM_SERVICE">
   <intent-filter>
     <action android:name=”android.service.dreams.DreamService” />
     <category android:name=”android.intent.category.DEFAULT” />
   </intent-filter>
 </service>           

如果不申請權限,這個互動屏保将無法啟動并有類似報錯:

system_process W/ActivityManager: Unable to start service Intent { act=android.service.dreams.DreamService flg=0x800000 cmp=com.google.android.deskclock/com.android.deskclock.Screensaver } U=0: not found

system_process E/DreamController: Unable to bind dream service: Intent { act=android.service.dreams.DreamService flg=0x800000 cmp=com.google.android.deskclock/com.android.deskclock.Screensaver }

system_process I/DreamController: Stopping dream: name=ComponentInfo{com.google.android.deskclock/com.android.deskclock.Screensaver}, isTest=false, canDoze=false, userId=0

demo

AndroidManifest.xml

注冊這個service;裡面指定的圖示和标題都顯示在設定中

<service
            android:name="com.rust.service.MyDayDream"
            android:exported="true"
            android:icon="@drawable/littleboygreen_x128"
            android:label="@string/my_day_dream_label"
            android:permission="android.permission.BIND_DREAM_SERVICE">
            <intent-filter>
                <action android:name="android.service.dreams.DreamService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>           

MyDayDream.java

互動屏保的定義

package com.rust.service;

import android.service.dreams.DreamService;

import com.rust.aboutview.R;

public class MyDayDream extends DreamService {
    
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        // Exit dream upon user touch
        setInteractive(false);
        // Hide system UI
        setFullscreen(true);
        // Set the dream layout
        setContentView(R.layout.my_day_dream);
    }

}           

my_day_dream.xml

互動屏保的布局檔案;隻有一行字

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_day_dream_label"
        android:textColor="@color/colorRed"
        android:textSize="30sp" />
</LinearLayout>           

在Settings-Display-Daydream中可以找到新增的選項

繼續閱讀