Android Daydream 互動屏保

Android Daydream 互動屏保

API19 API23
Create:2016-03-01

繼承DreamService來實現(xiàn)一個自定義屏保
Dreams是當充電的設(shè)備空閑,或者插入底座時顯示的互動屏保。在展覽或陳列時,Dreams為APP提供一個定制的展示方式。

DreamService的生命周期

1.onAttachedToWindow()
初始化設(shè)置,在這里可以調(diào)用 setContentView()

2.onDreamingStarted()
互動屏保已經(jīng)啟動,這里可以開始播放動畫或者其他操作

3.onDreamingStopped()
在停止 onDreamingStarted() 里啟動的東西
4.onDetachedFromWindow()
在這里回收前面調(diào)用的資源(比如 handlers 和 listeners)

另外,onCreate 和 onDestroy 也會被調(diào)用。但要復寫上面的幾個方法來執(zhí)行初始化和銷毀操作。

manifest 聲明

為了能讓系統(tǒng)調(diào)用,你的 DreamService 應(yīng)該在 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> 元素中。

通常提供的附加信息是對互動屏保的自定義設(shè)置,指向一個自己寫的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選項右邊會出現(xiàn)一個設(shè)置圖標。點擊此圖標可打開指定的activity。

當目標api>=21,必須在manifest中申請BIND_DREAM_SERVICE權(quán)限,比如:

 <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>

如果不申請權(quán)限,這個互動屏保將無法啟動并有類似報錯:
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;里面指定的圖標和標題都顯示在設(shè)置中

        <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中可以找到新增的選項

From: http://www.cnblogs.com/rustfisher/p/5232471.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容