一、通過手機發送通知給android手錶,首先需要將手機和手錶模擬器建立連接。
1.1 首先手機要下載android wear 中國版。
1.2 通過usb連接手機,打開android wear ,並且選擇模擬器。
1.3 在命令行輸入adb -d forward tcp:5601 tcp:5601
即可建立連接。
二、mobile下通過Notification Builder創建Notification。
int notificationId = 001;
Intent viewIntent = new Intent(this, MainActivity.class);
int eventId = 1;
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通過Notification Builder創建Notification")
.setContentText("張X峯到甪直幹活去了,太爽了!!!")
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
//獲取NotificationManager 服務的一個實例
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
// 使用notificationManager 來發出 Notification
notificationManager.notify(notificationId, notificationBuilder.build());
當Notification出現在手機上時,用戶可以通過點擊通知打開MainActivity;當Notification出現在穿戴設備上時,用戶可以左滑到open on phone 頁面,點擊打開MainActivity.
三、添加Action按鈕
int notificationId = 001;
Intent mainIntent = new Intent(this, MainActivity.class);
int eventId = 1;
mainIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
Intent viewIntent = new Intent(this, ViewEventActivity.class);
int eventId2 = 2;
viewIntent.putExtra(EXTRA_EVENT_ID, eventId2);
PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0, viewIntent, 0);;
//NotificationCompat.Style bigStyle;添加一個Big View
NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
String eventDescription = "額外擴展的內容:고마워 이토록 사랑해줘서," +
"완전 환자야 환장해 너만 볼 팔자야,工作 吹牛 石頭剪刀布三局兩勝," +
"일이고 나발이고 가위바위보 삼세판이고.";
bigStyle.bigText(eventDescription);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("添加Action按鈕")
.setContentText("信息")
.setContentIntent(mainPendingIntent)
.addAction(R.mipmap.map,
"地圖", viewPendingIntent)
.setStyle(bigStyle);
//獲取NotificationManager 服務的一個實例
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
// 使用notificationManager 來發出 Notification
notificationManager.notify(notificationId, notificationBuilder.build());
在穿戴設備上添加了一個action(類似於一個界面)。通過傳遞一個 PendingIntent 給 addAction() 來添加其它action(這個action也可以打開一個頁面)。添加了一個Big View。
四、可穿戴式獨有的 Actions
Intent actionIntent = new Intent(this, ViewEventActivity.class);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.mipmap.ic_action,
getString(R.string.label), actionPendingIntent)
.build();
// Build the notification and add the action via WearableExtender
Notification notification =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("可穿戴式獨有的 Actions")
.setContentText("開發者想要可穿戴式設備上的action與手持式設備不一樣的話," +
"可以使用 WearableExtender.addAction()," +
"一旦我們通過這種方式添加了action," +
"可穿戴式設備便不會顯示任何其他通過" +
" NotificationCompat.Builder.addAction() 添加的action。")
.extend(new NotificationCompat.WearableExtender().addAction(action))
.build();
//獲取NotificationManager 服務的一個實例
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
// 使用notificationManager 來發出 Notification
notificationManager.notify(notificationId, notification);
我認爲是另一種添加action的方法。
五、爲Notification添加可穿戴式特性
int notificationId = 001;
// Create a WearableExtender to add functionality for wearables
// BitmapFactory.Options newOpts = new BitmapFactory.Options();
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.flower);
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender()
.setHintHideIcon(true)
.setBackground(mBitmap);
// Create a NotificationCompat.Builder to build a standard notification
// then extend it with the WearableExtender
Notification notif = new NotificationCompat.Builder(this)
.setContentTitle("添加可穿戴式特性")
.setContentText("調用 setHintHideIcon() 方法把應用的圖標從 Notification 卡片上刪掉")
.setSmallIcon(R.mipmap.ic_launcher)
//.setLargeIcon(BitmapFactory.decodeResource(
// getResources(), R.mipmap.ic_launcher))這是把圖標放大,不適合背景
.extend(wearableExtender)
.build();
//獲取NotificationManager 服務的一個實例
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notif);
添加可穿戴設備特性;調用 setHintHideIcon() 方法把應用的圖標從 Notification 卡片上刪掉。