Android Wear究竟是什么?
Android Wear和手机是完全不同的设计理念:手机大多数情况下,都是用户去操作,去触发里面的应用;而手表则是“用户被动”的,用户越少感觉到你的存在,越是合理,而在某些场景下给予最合适的提醒,则是Android Wear最伟大的设计理念,这也是它想做到的。
Android Wear是智能的通知栏!(个人理解)
在早上醒来的时候,给你推送早上的天气、交通状况。在你到达餐馆的时候,给你推送大众点评,美团购物券,为你精打细算。检测到你的运动状态,显示计步器,心率状况(Android Wear本身内置)。
Android Wear开发阶段
产品需求方向的确定
很重要的是,产品要设计成什么样子?
1. 手机到手表的信息通知栏样式优化 : 例如设置背景
2. 手机与手表的智能推送:例如通过手表来寻找手机,或是将手机拍到的照片发送给手表
3. 自定义卡片样式:保持卡片样式的基础上,对卡片内的布局进行调整,添加动画,点击等交互
4. 完全自定义界面,提供独特的交互:可抛弃卡片样式,使用WearAPI提供的新组件
5. 手表表盘:各种样式,各种高大上
技术实现
以上4个点,是设计到不同方面,不同层次的技术。要想清楚后,使用最恰当的技术去实现之。以下列出针对上面的实现需要的技术点
1. 手表通知优化:实现最简单,是非常有效的提升应用在手表上的显示
2. 智能推送:数据通讯,监控手表的状态,为界面展示提供更丰富的数据支持
3. 自定义卡片:保留官方提供的卡片流样式,个性化修改卡片内容
4. 完全自定义界面:自己实现界面,需要处理多分辨率问题,自由度高
5. 手表表盘:类似手机动态壁纸的机制
开发平台的搭建
Eclipse平台的搭建
1. 下载最新版本的ADT,详情见官网:http://developer.android.com/sdk/installing/installing-adt.html 。(及早地迁移到Android Studio吧!)
2. 下载必要的SDK版本:最低4.4W.2(API20),也可以是5.0以上。注意不是4.4.2(API 19)。详情见截图。
1. SDK Platform:你懂的
2. Samples for SDK:强烈推荐下载,因为部分API在官网的开发者指导文章中是没有提及的,一些解决问题的思路,可以从SDKSamples中学习到。
3. 2个SystemImage:建议下载,要多分辨率测试,还是需要模拟器的!另外,务必升级到最新版本,在开发过程中,遇到模拟器Bug:动态加载的View在手表正常显示,而模拟器失效,更新后即可。
4. Sources for Android SDK:你懂的
3. 升级AndroidSDK Tools:最低23.0.0
4. 下载依赖库:
0. Google Support Library : 提供android.support.v4.app.NotificationCompat.WearableExtender:用于实现手表卡片通知的适配
1. Google Play Services:提供com.google.android.gms.wearable:用于实现手机和Wear的数据传输功能
2. Google Repository : 提供android.support.wearable:实现Wear的自定义界面
5. 解压Wear依赖库:下载后的包在/sdk/extras/google/m2repository/com/google/android/support/wearable/1.0.0/wearable-1.0.0.aar。解压到工作目录下,并将里面的classes.jar拷贝到libs目录下。
6. 导入Wear依赖库:Eclipse导入上一步解压的工程,并工程属性properties设置为Is Library.
创建Android Wear工程
ADT升级后,依然没有出现以上画面,因此,选择了以下的方式。
1. 下载模板工程:https://github.com/tangtungai/Android-Wear-Black-Activity-Template
2. Eclipse导入模板工程
3. 模板导入wearable-1.0.0依赖库,解决也许找不到android.support.wearable包的问题。
注:ADT 自(23.0.2)这个版本开始就没有Blank Wear Activity,所以得下载模板工程来开发。
创建模拟器
· Device:选择方形/圆形的设备
· Target:4.4w.2
· CPU/ABI:此处笔者选择Android Wear Intel Atom(X86),因为本人的64位Ubuntu 14.04系统在选择AndroidWear ARM(armeabi-v7a)时候会报错,所以在不确定哪个镜像会报错的情况下,最好两个都下载吧。
· Hardware keyboard present:手表是没有键盘输入的,所以,这里似乎没用,不过语音输入是否与之相关有待调查。
· Skin:选择与Device对应的,AndroidWearSquare
· Use Host GPU:这个很关键,不勾选这个,在做Notification显示自定义的Activity的时候会看到一片漆黑
Android Studio 平台的搭建
在Android Studio中创建新的工程(包名,工程名你随便起了),Next
同时创建手机项目和Wear项目
创建Phone端的页面样式
创建Wear端的页面样式
同时生成Phone和Wear项目(方便打包)
Wear的连接调试
步骤是:
1. 手机端usb调试开关
2. 在Android Wear手表设备上开启蓝牙调试开关:
–>原生AndroidWear系统是:连点两次主屏会显示出wear菜单或者长按手表右侧物理按键,滚动到底部点击设置,再滚动动到底部,如果也没有开发者选项,就点击关于,再点击系统版本信息七次,就会出现开发者选项。进入开发者选项,并开启蓝牙调试开关。
–>Ticwear中文系统上是主屏上左划出现菜单,滚动选择设置,滚动到底部,如果也没有开发者选项,同样,就点击关于,再点击系统版本信息七次,就会出现开发者选项。进入开发者选项,并开启蓝牙调试开关。
3. 开启调试会话:
1)手机USB数据线连接到电脑,在手机端打开Android Wear应用(确保应 用已连接到手表)。
2)点击界面右上的齿轮菜单进入设置(Settings)
3)开启“通过蓝牙调试”(Debuggingover Bluetooth)。你可以看到设备连接状态:
Host:disconnected
Target: connected
4)确保手机用USB数据线连接了电脑,并执行adb命令:
adb forward tcp:4444 localabstract:/adb-hub;
adb connect localhost:4444
你就可以看到刚才的连接状态就变成了:
Host: connected
Target: connected
4. 调试应用
如果执行adb devices命令,你的可穿戴设备应该是显示为localhost:4444。要执行任何adb命令,格式应该是类似这样:
adb -s localhost:4444 <command>
如果没有其他设备通过TCP/IP连接(即没有连接其他模拟器),你也可以使用简化的命令:
adb -e <command>
例如:
adb -e logcat
adb -e shell
adb -e bugreport
注:其实第3步OK后用AndroidStudio直接跑起Wear程序即可,App即可会自动安装到手表上,稍微有点慢耐心等下就好。
Wear APP的正式开发
与手机端开发的一些不同
- 系统有个强制休眠时间。如果正在显示界面,但用户又没有进行操作,设备就会休眠。当再次唤醒后,会回到表盘主页而不是你之前的界面。如果你有东西需要持久化显示,可以推送通知到信息流中。
- android wear应用相对于手机应用来说,显示尺寸和功能更小巧。可能是手机应用的子集,通常你可以先在手机上操作,然后将结果发送到手表。
- 用户不需要直接下载应用到android wear设备。你只需要将android wear应用绑定到android手机应用中。当用户安装了手机应用,系统会自动安装android wear应用。可以出于开发目的,你依然可以安装应用到android wear设备中。
4. android wear应用可以访问标准的AndroidAPIs。但不支持一下APIs:
Wear 通知的开发
为了创建一个手持设备上的并且也能同时发送给可穿戴设备的Notification,需要使用NotificationCompat.Builder。当你使用这个类创建Notification之后,如何正确展示的工作就交由系统去完成,无论是在手持式设备上还是可穿戴设备上。
需要导入的类(手机端)
importandroid.support.v4.app.NotificationCompat;
importandroid.support.v4.app.NotificationManagerCompat;
importandroid.support.v4.app.NotificationCompat.WearableExtender;
普通通知栏
- 手机:普通的通知栏在手机上的效果应该都不陌生,这里就不展开说明
- 手表:手表端的效果是由2张卡片构成的,第一张是手机通知栏的信息组成,第二张是点击开发手机应用,具体的效果与手机通知栏的点击事件一致,也就是说,如果通知栏没有设置点击事件,那么就不会有第二张卡片。另外,默认的背景色是由应用图标所决定的,是取主要的颜色值
以上是最原始的通知栏效果,没有进行手表端适配处理的。(其实普通的通知栏你都不用做。。。。。。)
添加Wear扩展属性的通知栏
扩展属性
· 多张卡片:通知栏的多张内容展示
· 自定义动作按钮:自定义Action按钮的事件处理
· 设置背景:设置通知栏的背景色
· 堆叠多张卡片:通知栏卡片的堆叠
· 语音回复:
Wear 上的UI开发
Android Wear apps用户界面不同于在手机设备上构建。需要遵循AndroidWear设计规范和UI模式,以确保应用通过针对可穿戴设备而优化的一致用户体验。
UI模式主要通过以下方式实现:
- 卡片
- 倒计时和确认
- 长按消失
- 2d pickers
- 选择列表
Wearable UI Library是Android SDk中Google Repository的一部分,提供帮助你实现这些模式的类,并可以创建同时适配运行在圆形和方形屏幕设备上的布局。
定义布局
当创建android wear布局的时候,要考虑设备有两种屏幕,方形和圆形。任何位于屏幕角落的内容都会被圆形屏幕裁剪掉。
Wearable UI Library提供两种方式解决这个问题:
1、为圆形和方形屏幕的设备定义两套布局。在运行时检测设备屏幕并渲染不同的布局。
2、使用Wearable UILibrary中一种特殊的布局来包住你的布局。这个布局会根据设备屏幕来加载不同的布局文件。
典型的办法是第一种,如果布局简单可以直接使用第二种。
为圆形和方形屏幕定义不同的布局
Wearable UI Library中的WatchViewStub这个类可以让你为圆形和方形屏幕定义不同的布局。这个类会在运行时检测屏幕形状并渲染相应的布局。
1、在你的activity布局文件中添加WatchViewStub元素
2、使用rectLayout属性为方形屏幕指定布局文件
3、使用roundLayout属性为圆形屏幕指定布局文件
- <android.support.wearable.view.WatchViewStub
- xmlns:android=”http://schemas.android.com/apk/res/android”
- xmlns:app=”http://schemas.android.com/apk/res-auto”
- xmlns:tools=”http://schemas.android.com/tools”
- android:id=”@+id/watch_view_stub”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”
- app:rectLayout=”@layout/rect_activity_wear”
- app:roundLayout=”@layout/round_activity_wear”>
- </android.support.wearable.view.WatchViewStub>
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_wear);
- }
- 访问布局控件
- 渲染布局文件不是同步的,所以要设置一个回调来监听WatchViewStub渲染完成。
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_wear);
- WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
- stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
- @Override public void onLayoutInflated(WatchViewStub stub) {
- // Now you can access your views
- TextView tv = (TextView) stub.findViewById(R.id.text);
- …
- }
- });
- }
<android.support.wearable.view.WatchViewStub xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/watch_view_stub" android:layout_width="match_parent" android:layout_height="match_parent" app:rectLayout="@layout/rect_activity_wear" app:roundLayout="@layout/round_activity_wear"> </android.support.wearable.view.WatchViewStub> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wear); } 访问布局控件 渲染布局文件不是同步的,所以要设置一个回调来监听WatchViewStub渲染完成。 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wear); WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) { // Now you can access your views TextView tv = (TextView) stub.findViewById(R.id.text); ... } }); }
创建卡片
卡片风格可以让不同应用呈现给用户一致的风格和交互。Wearable UI库中实现了为可穿戴设备特殊设计的卡片控件。这个库包含了CardFrame类,CardFrame只能包含一个直接子view,你可以添加其他自定义内容插入到卡片中。
你有两种办法来添加卡片:
1、使用或继承CardFragment2、在你的布局中添加CardScrollView
创建CardFragment
CardFragment 提供一个默认卡片布局,包含标题、图标、描述。
创建CardFragment的步骤:
1、在你的布局中,分配一个id给CardFragment
2、在你的activity中新建一个CardFragment实例
3、使用fragmentmanager添加CardFragment实例
- <android.support.wearable.view.BoxInsetLayout
- xmlns:android=“http://schemas.android.com/apk/res/android”
- xmlns:app=“http://schemas.android.com/apk/res-auto”
- android:background=“@drawable/robot_background”
- android:layout_height=“match_parent”
- android:layout_width=“match_parent”>
- <FrameLayout
- android:id=“@+id/frame_layout”
- android:layout_width=“match_parent”
- android:layout_height=“match_parent”
- app:layout_box=“bottom”>
- </FrameLayout>
- </android.support.wearable.view.BoxInsetLayout>
<android.support.wearable.view.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@drawable/robot_background" android:layout_height="match_parent" android:layout_width="match_parent"> <FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_box="bottom"> </FrameLayout> </android.support.wearable.view.BoxInsetLayout>
Activity
代码中:
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_wear_activity2);
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- CardFragment cardFragment = CardFragment.create(getString(R.string.cftitle),
- getString(R.string.cfdesc),
- R.drawable.p);
- fragmentTransaction.add(R.id.frame_layout, cardFragment);
- fragmentTransaction.commit();
- }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wear_activity2); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); CardFragment cardFragment = CardFragment.create(getString(R.string.cftitle), getString(R.string.cfdesc), R.drawable.p); fragmentTransaction.add(R.id.frame_layout, cardFragment); fragmentTransaction.commit(); }
如果要使用CardFragment来创建一个自定义布局的卡片,可以继承这个类然后重写onCreateContentView方法。
添加CardFrame 到你的布局中
你也可以直接添加一个卡片到你的布局中。
- <android.support.wearable.view.BoxInsetLayout
- xmlns:android=“http://schemas.android.com/apk/res/android”
- xmlns:app=“http://schemas.android.com/apk/res-auto”
- android:background=“@drawable/robot_background”
- android:layout_height=“match_parent”
- android:layout_width=“match_parent”>
- <android.support.wearable.view.CardScrollView
- android:id=“@+id/card_scroll_view”
- android:layout_height=“match_parent”
- android:layout_width=“match_parent”
- app:layout_box=“bottom”>
- <android.support.wearable.view.CardFrame
- android:layout_height=“wrap_content”
- android:layout_width=“fill_parent”>
- <LinearLayout
- android:layout_height=“wrap_content”
- android:layout_width=“match_parent”
- android:orientation=“vertical”
- android:paddingLeft=“5dp”>
- <TextView
- android:fontFamily=“sans-serif-light”
- android:layout_height=“wrap_content”
- android:layout_width=“match_parent”
- android:text=“@string/custom_card”
- android:textColor=“@color/black”
- android:textSize=“20sp”/>
- <TextView
- android:fontFamily=“sans-serif-light”
- android:layout_height=“wrap_content”
- android:layout_width=“match_parent”
- android:text=“@string/description”
- android:textColor=“@color/black”
- android:textSize=“14sp”/>
- </LinearLayout>
- </android.support.wearable.view.CardFrame>
- </android.support.wearable.view.CardScrollView>
- </android.support.wearable.view.BoxInsetLayout>
<android.support.wearable.view.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/robot_background"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.wearable.view.CardScrollView
android:id="@+id/card_scroll_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_box="bottom">
<android.support.wearable.view.CardFrame
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp">
<TextView
android:fontFamily="sans-serif-light"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/custom_card"
android:textColor="@color/black"
android:textSize="20sp"/>
<TextView
android:fontFamily="sans-serif-light"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/description"
android:textColor="@color/black"
android:textSize="14sp"/>
</LinearLayout>
</android.support.wearable.view.CardFrame>
</android.support.wearable.view.CardScrollView>
</android.support.wearable.view.BoxInsetLayout>
CardScrollView 元素可以让你设置卡片的gravity。
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_wear_activity2);
- CardScrollView cardScrollView =
- (CardScrollView) findViewById(R.id.card_scroll_view);
- cardScrollView.setCardGravity(Gravity.BOTTOM);
- }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wear_activity2); CardScrollView cardScrollView = (CardScrollView) findViewById(R.id.card_scroll_view); cardScrollView.setCardGravity(Gravity.BOTTOM); }
CardScrollView 会检测设备的屏幕形状,所以在方形和圆形屏幕上显示卡片的方式不同。可以在外面嵌套一个BoxInsetLayout。
创建列表
列表可以让用户在一系列选项中选择某一项。WearableUI库包含WearableListView类,是专为穿戴设备优化的实现类。
创建列表的步骤:
1、在你activity的布局中添加WearableListView元素
2、创建一个自定义布局来实现你的list item
3、创建一个adapter装载进这个listview
添加一个listview
- <android.support.wearable.view.BoxInsetLayout
- xmlns:android=“http://schemas.android.com/apk/res/android”
- xmlns:app=“http://schemas.android.com/apk/res-auto”
- android:background=“@drawable/robot_background”
- android:layout_height=“match_parent”
- android:layout_width=“match_parent”>
- <FrameLayout
- android:id=“@+id/frame_layout”
- android:layout_height=“match_parent”
- android:layout_width=“match_parent”
- app:layout_box=“left|bottom|right”>
- <android.support.wearable.view.WearableListView
- android:id=“@+id/wearable_list”
- android:layout_height=“match_parent”
- android:layout_width=“match_parent”>
- </android.support.wearable.view.WearableListView>
- </FrameLayout>
- </android.support.wearable.view.BoxInsetLayout>
<android.support.wearable.view.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/robot_background"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_box="left|bottom|right">
<android.support.wearable.view.WearableListView
android:id="@+id/wearable_list"
android:layout_height="match_parent"
android:layout_width="match_parent">
</android.support.wearable.view.WearableListView>
</FrameLayout>
</android.support.wearable.view.BoxInsetLayout>
其他的ListView绑定数据显示在这里就不啰嗦了
Wear 数据层的通信
android wear数据层API,是Google Play services的一部分,提供手持设备和android wear应用的通信通道。这个API包含一个数据对象集合和数据层重要事件的监听:
Data Items
一个DataItem提供数据存储和手持设备与android wear设备间的自动同步
Messages
MessageApi类可以发送消息,可用于远程过程调用(RPC),例如通过android wear设备来控制手持设备上的媒体播放器,或者从手持设备上向android wear设备发起一个intent。
消息也可以用于单向请求或者一个请求/响应交流模型。如果手持设备和android wear设备已连接,系统会按顺序发送消息并返回一个成功的结果状态。如果设备间未连接,则返回错误。一个成功的结果状态不能表明消息发送成功,也可能是接受到结果状态后设备间断开了连接。
Asset
Asset对象是为了发送二进制原始数据,例如一张图片。你需要附加assets给data items,并且系统自动会帮你传输,通过缓存大的assets保护蓝牙带宽来避免重新传输。
WearableListenerService(针对service)
继承WearableListenerService可以让你在service中监听重要的数据层事件,系统会管理WearableListenerService的生命周期,当需要传输data items或消息时来bindservice,当不需要再工作时来unbind service。
DataListener(针对前台界面)
在一个前台Activity中实现DataListener接口可以让你监听重要的数据层事件。
注意:因为这些APIs是为手持设备和android wear设备通信而设计的,这是唯一用来在这些设备间通信的APIs。具体来说,不要尝试打开底层sockets来创建通信通道。