Android Wear開發 - 卡片通知 - 第一節 : 添加Android Wear通知特性


一. 前言說明

Android Wear大部分顯示形式是卡片的形式,而最簡單地支持Android Wear方式就是用通知**Notification**。而實現最簡單的,非高度自定義的通知,則只需要在手機端做一些處理,不需要進行手表端應用的開發,可以說是開發成本特別低。
本節主要講解的就是簡單的Wear特色卡片通知。

二. 環境配置

  1. 工程引用新版的Support-V4。
  2. 使用到的主要有以下3個類。
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;

三. 普通的通知欄

效果圖

  1. 手機:普通的通知欄在手機上的效果應該都不陌生,這里就不展開說明
  2. 手表:手表端的效果是由2張卡片構成的,第一張是手機通知欄的信息組成,第二張是點擊開發手機應用,具體的效果與手機通知欄的點擊事件一致,也就是說,如果通知欄沒有設置點擊事件,那么就不會有第二張卡片。另外,默認的背景色是由應用圖標所決定的,是取主要的顏色值。

代碼實現


public void sendClassicNotify() {

	Notification.Builder builder = new Notification.Builder(this);

	// 1.設置顯示信息

	builder.setContentTitle("標題");

	builder.setContentText("內容");

	builder.setSmallIcon(R.drawable.ic_launcher);



	// 2.設置點擊跳轉事件,若不設置,則手表沒有第二張卡片

	Intent intent = new Intent(this, PhoneActivity.class);

	PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,

			intent, 0);

	builder.setContentIntent(pendingIntent);



	// 3.設置通知欄其他屬性

	builder.setAutoCancel(true);

	builder.setDefaults(Notification.DEFAULT_ALL);



	NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

	manager.notify(0, builder.build());

}

問題

以上是最原始的通知欄效果,沒有進行手表端適配處理的。看起來還不錯,但會存在什么問題呢?
比如下面的效果,如果內容太長,就會很難看,這只是一部分,它還可以繼續滾動。

四. 添加Wear擴展屬性的通知欄

效果圖

擴展屬性

  • 多張卡片:如第一張圖片的第二張卡片
  • 自定義動作按鈕:如第一張圖片的第二張卡片
  • 設置背景:如第一張圖片的的背景
  • 堆疊多張卡片:如第二張圖片的第一張卡片
  • 語音回復:如第二張圖片的第二張卡片

代碼實現


public void sendWearNotify() {

	NotificationCompat.Builder builder = new NotificationCompat.Builder(

			this);

	// 1.設置顯示內容

	builder.setContentTitle("消息標題");

	builder.setContentText("消息內容");

	// 若只設置了SmallIcon,而沒設置LargeIcon,則在通知欄左側會顯示SmallIcon設置的圖標;若同時設置了LargeIcon,則左側顯示LargeIcon,右側顯示SmallIcon

	builder.setSmallIcon(R.drawable.ic_launcher);

	// 若設置了LargeIcon,則Wear背景會變成LargeIcon.

	// builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),

	// R.drawable.default_background_sunny_day_bg));



	// 2.設置跳轉屬性

	Intent intent = new Intent(this, PhoneActivity.class);

	PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,

			intent, 0);

	// 設置了ContentIntent后,通知欄有了點擊效果,而wear滑動到最右側時,多了一個Open on phone的頁面

	builder.setContentIntent(pendingIntent);



	// 3.設置通知屬性

	builder.setAutoCancel(true);

	builder.setDefaults(Notification.DEFAULT_ALL);



	// 4.設置手表特有屬性

	builder.extend(extendWear(builder));



	mNotificationManager.notify(1, builder.build());

}

接下來對以上代碼第四點(4.設置手表特有屬性)進行拓展。

0. 方法結構


private NotificationCompat.WearableExtender extendWear(

		NotificationCompat.Builder builder) {

	NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();


    //TODO:具體屬性設置代碼


	return wearableExtender;


} 

接下來的功能點都插入在TODO后面。

1. 設置背景


	BitmapFactory.Options options = new BitmapFactory.Options();

	// 官方文檔提示 : http://developer.android.com/training/wearables/notifications/creating.html	

	/**

	 * Note: The bitmap that you use with setBackground() should have a

	 * resolution of 400x400 for non-scrolling backgrounds and 640x400 for

	 * backgrounds that support parallax scrolling. Place these bitmap

	 * images in the res/drawable-nodpi directory of your handheld app.

	 * Place other non-bitmap resources for wearable notifications, such as

	 * those used with the setContentIcon() method, in the res/drawable-hdpi

	 * directory of your handheld app.

	 */

    // 可滾動,背景則為640x400,否則為400x400.

	// 若設置了背景,則LargeIcon在Wear端失效

	options.outWidth = 640;

	options.outHeight = 400;

	Bitmap wearBgBitmap = BitmapFactory.decodeResource(getResources(),

			R.drawable.wallpaper_1, options);

	// 設置了Bg后,LargeIcon便失效

	wearableExtender.setBackground(wearBgBitmap);

  • 圖片存放位置:通用的圖標放在res/hdpi目錄下,Wear獨有的圖片放在res/nodpi目錄下
  • 背景圖片大小:可滾動的背景則為640x400,不可滾動的尺寸400x400 (px為單位).
  • 屬性沖突:使用了setBackground方法,同時設置了builder.setLargeIcon方法,那么手機顯示的是setLargeIcon的,而手表顯示的為setBackground的。

2. 添加獨有的Action按鈕


	// 2. 為Wear添加獨有的action

	Intent intent = new Intent(this, PhoneActivity.class);

	PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,

			intent, 0);

	NotificationCompat.Action action = new NotificationCompat.Action(

			R.drawable.action, "新的Action", pendingIntent);

	wearableExtender.addAction(action);

3. 調整首頁圖標


	// 3. 調整首頁的圖標

	// 隱藏默認應用圖標

	wearableExtender.setHintHideIcon(true);

	// 設置跟ContentText跟隨的圖標

	wearableExtender.setContentIcon(R.drawable.mycolors);

	// 只支持Start和End標簽,默認是End

	wearableExtender.setContentIconGravity(Gravity.START);

  • 設置setHintHideIcon為true后,原本右上角的應用圖標便會隱藏。
  • Gravity只能設置Start和End標簽,前者對應Left,后者對應Right。

筆者之前做過RTL(右到左語言)的適配,如阿拉伯語,波斯語等,就是從右向左讀的語言,要把整個界面的左對齊變成右對齊,而Android 4.2提供了Start和End標簽,用來自動適配對齊方式,在LTR(左到右)語言中,Start→Left,End→Right;而在RTL(右到左)語言中,Start→Right,End→Left。我們這里就不展開來講了,之后有時間會將筆記上傳博客,有興趣的同學屆時可以參考下。

4. 添加第二頁卡片


	// 4. 添加第二頁卡片

	Notification secondPageNotification = new NotificationCompat.Builder(

			this).setContentTitle("page 2")

			.setContentText("現在顯示的Wear通知內容,測試可以顯示多少內容,試試看,哈哈哈.繼續來一個")

			.build();

	wearableExtender.addPage(secondPageNotification);

關於堆疊多張卡片語音回復的內容,由於較少用到,筆者就沒繼續研究。
若讀者有興趣,可上開發者官網學習,其實也不難。
堆疊多張卡片地址
語音回復地址

以上就是筆者在做簡單卡片適配的總結,更多屬性的使用,可以閱讀Samples for SDK目錄下的wear/Notifications工程,這個Sample在通知欄基本上的所有功能都有涉及到。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM