為穿戴式設(shè)備創(chuàng)建布局和在手機(jī)上的操作一樣,不過(guò)為了整體平衡和諧感你需要根據(jù)屏幕尺寸進(jìn)行設(shè)計(jì).直接使用手機(jī)上的UI,用戶體驗(yàn)不是很好.閱讀設(shè)計(jì)指導(dǎo)獲取如何設(shè)計(jì)優(yōu)秀穿戴式app的信息.
創(chuàng)建自定義通知
一般情況下你應(yīng)該在手持設(shè)備上創(chuàng)建通知并自動(dòng)同步到穿戴式設(shè)備上.這可以使你僅構(gòu)建一次通知而展現(xiàn)在多種不同設(shè)備上(不僅是穿戴設(shè)備上,還有電視),從而避免多次的設(shè)計(jì).
如果標(biāo)準(zhǔn)的通知樣式(比如:NotificationCompat.BigTextStyle或者NotificationCompat.InboxStyle)不能滿足你的工作需求,你可以使用自定義布局去展示.你只能在穿戴式設(shè)備上創(chuàng)建和展示自定義通知,系統(tǒng)不會(huì)同步到手持設(shè)備上.
注意:當(dāng)在穿戴式設(shè)備上創(chuàng)建自定義通知時(shí),你可以用標(biāo)準(zhǔn)通知的API(API 20)代替支持庫(kù).
創(chuàng)建自定義通知
1.為Activity的內(nèi)容創(chuàng)建一個(gè)自定義布局視圖
public void onCreate(Bundle bundle){
...
setContentView(R.layout.notification_activity);
}
2.在清單文件中為Activity定義必要的屬性,使其可以展示在穿戴式設(shè)備上下文中.你需要聲明Activity的以下屬性:exportable
,allowEmbedded
taskAFFinity
,另外推薦使用Theme.DeviceDefault.Light
主題.
<activity android:name="com.example.MyDisplayActivity"
android:exported="true"
android:allowEmbedded="true"
android:taskAffinity=""
android:theme="@android:style/Theme.DeviceDefault.Light" />
3.為希望展示的Activity創(chuàng)建隱式意圖
Intent notificationIntent = new Intent(this, NotificationActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(
this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4.建立一個(gè)Notification并調(diào)用setDisplayIntent()方法.當(dāng)用戶觸發(fā)點(diǎn)擊這個(gè)通知后系統(tǒng)就會(huì)啟動(dòng)這個(gè)隱式意圖.
5.用[notify](https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification))方法展示通知.
當(dāng)通知在主屏幕上展示時(shí),系統(tǒng)會(huì)用標(biāo)準(zhǔn)模版展示它.當(dāng)用戶向上滑動(dòng)這個(gè)通知時(shí),將會(huì)看到為通知設(shè)置的自定義視圖.
使用Wearable UI Library創(chuàng)建布局
Wearable UI Library這個(gè)庫(kù)在創(chuàng)建可穿戴式app的時(shí)候會(huì)被自動(dòng)包括.也可以在在build.gradle
中添加這個(gè)庫(kù).
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.support:wearable:+'
compile 'com.google.android.gms:play-services-wearable:+'
}
這個(gè)庫(kù)幫助你構(gòu)建UI,更多信息可以參考Creating Custom UIs for Wear Devices.下面是這個(gè)類庫(kù)中一些主要的類.