Android Daydream 互動(dòng)屏保
API19 API23
Create:2016-03-01
繼承DreamService來實(shí)現(xiàn)一個(gè)自定義屏保
Dreams是當(dāng)充電的設(shè)備空閑锅尘,或者插入底座時(shí)顯示的互動(dòng)屏保鸯匹。在展覽或陳列時(shí),Dreams為APP提供一個(gè)定制的展示方式漂羊。
DreamService的生命周期
1.onAttachedToWindow()
初始化設(shè)置蹬屹,在這里可以調(diào)用 setContentView()
2.onDreamingStarted()
互動(dòng)屏保已經(jīng)啟動(dòng)侣背,這里可以開始播放動(dòng)畫或者其他操作
3.onDreamingStopped()
在停止 onDreamingStarted() 里啟動(dòng)的東西
4.onDetachedFromWindow()
在這里回收前面調(diào)用的資源(比如 handlers 和 listeners)
另外,onCreate 和 onDestroy 也會(huì)被調(diào)用慨默。但要復(fù)寫上面的幾個(gè)方法來執(zhí)行初始化和銷毀操作贩耐。
manifest 聲明
為了能讓系統(tǒng)調(diào)用,你的 DreamService 應(yīng)該在 APP 的 manifest 中注冊(cè):
<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>
元素中潮太。
通常提供的附加信息是對(duì)互動(dòng)屏保的自定義設(shè)置,指向一個(gè)自己寫的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àng)右邊會(huì)出現(xiàn)一個(gè)設(shè)置圖標(biāo)消别。點(diǎn)擊此圖標(biāo)可打開指定的activity抛蚤。
當(dāng)目標(biāo)api>=21,必須在manifest中申請(qǐng)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>
如果不申請(qǐng)權(quán)限岁经,這個(gè)互動(dòng)屏保將無法啟動(dòng)并有類似報(bào)錯(cuò):
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
注冊(cè)這個(gè)service;里面指定的圖標(biāo)和標(biāo)題都顯示在設(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
互動(dòng)屏保的定義
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
互動(dòng)屏保的布局文件蛇券;只有一行字
<?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中可以找到新增的選項(xiàng)