[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YDRiKjYy-1637431867561)(/)]
创建Phone端的页面样式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b61gQOVj-1637431867600)(/)]
创建Wear端的页面样式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ljuMwVKJ-1637431867601)(/)]
同时生成Phone和Wear项目(方便打包)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JfMIvxFs-1637431867602)(/)]
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
如果没有其他设备通过TCP/IP连接(即没有连接其他模拟器),你也可以使用简化的命令:
adb -e
例如:
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:
android.webkit
android.print
android.app.backup
android.appwidget
android.hardware.usb
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属性为圆形屏幕指定布局文件
[java]
view plain
copy
?
<android.support.wearable.view.WatchViewStub
xmlns:android=”/apk/res/android”
xmlns:app=”/apk/res-auto”
xmlns:tools=”/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
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear);
}
访问布局控件
渲染布局文件不是同步的,所以要设置一个回调来监听WatchViewStub渲染完成。
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layo
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享
ut.activity_wear);
WatchViewStubstub=(WatchViewStub)findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(newWatchViewStub.OnLayoutInflatedListener(){
@OverridepublicvoidonLayoutInflated(WatchViewStubstub){
//Nowyoucanaccessyourviews
TextViewtv=(TextView)stub.findViewById(R.id.text);
…
}
});
}
<android.support.wearable.view.WatchViewStub
xmlns:android=“/apk/res/android”
xmlns:app=“/apk/res-auto”
xmlns:tools=“/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 提供一个默认卡片布局,包含标题、图标、描述。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QUMD0HmJ-1637431867603)(/)]
创建CardFragment的步骤:
1、在你的布局中,分配一个id给CardFragment
2、在你的activity中新建一个CardFragment实例
3、使用fragmentmanager添加CardFragment实例
[html]
view plain
copy
?
<android.support.wearable.view.BoxInsetLayout
xmlns:android=“/apk/res/android”
xmlns:app=“/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=“/apk/res/android”
xmlns:app=“/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”>
</android.support.wearable.view.BoxInsetLayout>
Activity``代码中:
[java]
view plain
copy
?
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear_activity2);
FragmentManagerfragmentManager=getFragmentManager();
FragmentTransactionfragmentTransaction=fragmentManager.beginTransaction();
CardFragmentcardFragment=CardFragment.create(getString(R.string.cftitle),
getString(R.string.cfdesc),
R.drawable.p);
fragmentTransaction.add(R.id.frame_layout,cardFragment);
mit();
}
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);
mit();
}
如果要使用CardFragment来创建一个自定义布局的卡片,可以继承这个类然后重写onCreateContentView方法。
添加CardFrame 到你的布局中
你也可以直接添加一个卡片到你的布局中。
[html]view plain copy
print ?
<android.support.wearable.view.BoxInsetLayout
xmlns:android=“/apk/res/android”
xmlns:app=“/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=“/apk/res/android”
xmlns:app=“/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”/>
</android.support.wearable.view.CardFrame>
</android.support.wearable.view.CardScrollView>
</android.support.wearable.view.BoxInsetLayout>
CardScrollView 元素可以让你设置卡片的gravity。
[java]
view plain
copy
?
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear_activity2);
CardScrollViewcardScrollView=
(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);
}
</android.support.wearable.view.CardScrollView>
</android.support.wearable.view.BoxInsetLayout>
CardScrollView 元素可以让你设置卡片的gravity。
[java]
view plain
copy
?
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear_activity2);
CardScrollViewcardScrollView=
(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);
}