一、通过手机发送通知给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 卡片上删掉。