Today, we’ll be discussing Android Wear with a basic hello world application using Android Studio and a Wear OS Emulator.

今天,我们将使用Android Studio和Wear OS模拟器与基本的hello world应用程序讨论Android Wear。

Android Wear应用类型 (Android Wear App Types)

Android Wear Apps can be of two types:

Android Wear Apps可以有两种类型:

  • Standalone App独立应用
  • Companion App – this has an equivalent phone app which can communicate with the wear app.伴侣应用程序 –具有等效的电话应用程序,可以与穿戴应用程序进行通信。

Let’s start by creating a Simple Standalone Wear OS App in our Android Studio Project.

让我们从在Android Studio项目中创建一个简单的独立Wear OS应用程序开始。

Android Wear Hello World应用程序入门 (Getting Started with Android Wear Hello World App)

Choose Wear OS project template from the wizard as shown below:

从向导中选择Wear OS项目模板,如下所示:

Android Wear Hello World Getting Started 1

Android Wear Hello World入门1

Android Wear Hello World Getting Started 2

Android Wear Hello World入门2

项目结构 (Project Structure)

This how our project structure looks like:

我们的项目结构如下所示:

Android Wear Hello World Project Structure

Android Wear Hello World项目结构

The dependencies for wear in the build.gradle are:

build.gradle中的磨损依赖项为:

dependencies {implementation 'com.google.android.support:wearable:2.4.0'implementation 'com.google.android.gms:play-services-wearable:16.0.1'implementation 'com.android.support:percent:28.0.0'implementation 'com.android.support:support-v4:28.0.0'implementation 'com.android.support:recyclerview-v7:28.0.0'implementation 'com.android.support:wear:28.0.0'compileOnly 'com.google.android.wearable:wearable:2.4.0'
}

AndroidManifest.xml (AndroidManifest.xml)

The Wear OS App’s manifest is slightly different from the normal Android Phone Apps.
Here, we need to specify the feature and wear OS library along with some metadata.

Wear OS App的清单与普通的Android Phone Apps略有不同。
在这里,我们需要指定功能并安装OS库以及一些元数据。

<manifest xmlns:android="https://schemas.android.com/apk/res/android"package="com.journaldev.androidwearoshelloworld"><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-feature android:name="android.hardware.type.watch" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@android:style/Theme.DeviceDefault"><uses-libraryandroid:name="com.google.android.wearable"android:required="true" /><!--Set to true if your app is Standalone, that is, it does not require the handheld app to run.--><meta-dataandroid:name="com.google.android.wearable.standalone"android:value="true" /><activityandroid:name=".MainActivity"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

布局 (Layout)

The most common layout used for smartwatches having wear os is BoxInsetLayout.

具有磨损os的智能手表最常用的布局是BoxInsetLayout

<android.support.wearable.view.BoxInsetLayout>...<LinearLayout...app:layout_box="all">
</android.support.wearable.view.BoxInsetLayout>

Another layout commonly used is SwipeDismissFrameLayout. This enables swipe from left to right.

另一个常用的布局是SwipeDismissFrameLayout 。 这样可以从左向右滑动。

The RecyclerView equivalent class for wear os is WearableRecyclerView.

磨损os的RecyclerView等效类是WearableRecyclerView

android.support.wearable.view.CircledImageView provides a circular layout to display the image.

android.support.wearable.view.CircledImageView提供圆形布局以显示图像。

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayoutxmlns:android="https://schemas.android.com/apk/res/android"xmlns:app="https://schemas.android.com/apk/res-auto"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/dark_grey"android:padding="@dimen/box_inset_layout_padding"tools:context=".MainActivity"tools:deviceIds="wear"><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:padding="@dimen/inner_frame_layout_padding"app:boxedEdges="none"><android.support.wear.widget.SwipeDismissFrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/swipe_dismiss_root" ><TextViewandroid:id="@+id/test_content"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="bottom"android:text="Swipe the screen to dismiss me." /></android.support.wear.widget.SwipeDismissFrameLayout><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/hello_world" /></FrameLayout>
</android.support.wear.widget.BoxInsetLayout>

Here we’ve added a swipe to dismiss layout with a text view in it.

在这里,我们添加了滑动以关闭带有文本视图的布局。

码 (Code)

The code for the MainActivity.java class is given below:

MainActivity.java类的代码如下:

package com.journaldev.androidwearoshelloworld;import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.widget.TextView;public class MainActivity extends WearableActivity {private TextView mTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextView = findViewById(R.id.text);// Enables Always-onsetAmbientEnabled();}
}

The always-on feature allows the app to control what to show on the watch while it’s in ambient mode.

常亮功能使应用可以控制在环境模式下手表上的显示内容。

Let’s look at our Hello World Wear OS Application when run on the emulator:

让我们看一下在模拟器上运行的Hello World Wear OS应用程序:

Android Wear Os Hello World Output

Android Wear Os Hello World输出

That brings an end to this Hello World Wear OS Android Tutorial.
You can download the project from the link below or view the full source code in the Github Repository.

这样就结束了本《 Hello World Wear OS Android教程》。
您可以从下面的链接下载项目,或在Github存储库中查看完整的源代码。

AndroidWearOSHelloWorldAndroidWearOSHelloWorld
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/28956/android-wear-hello-world

Android Wear Hello World相关推荐

  1. android wear无法启用,android-wear – 无法创建Android虚拟设备,“没有为此目标安装系统映像”...

    为了创建一个Android穿戴式模拟器,你需要按照下面的说明, 1.如果您的Android SDK工具版本低于22.6,您必须更新 2.在Android 4.4.2下,选择Android Wear A ...

  2. android wear 上网,Android Wear 2.0智能回复不需联网?这是最新的离线AI技术

    日前,谷歌终于发布了姗姗来迟的 Android Wear 2.0,其中最大的更新除了允许手表脱离手机直接使用之外,当属"智能回复"(Smart Reply)了.而根据谷歌最新发布的 ...

  3. android wear 兼容问题,【悲剧了】仅 1/4 安卓手机兼容 Android Wear - 爱应用

    在不久前举行的谷歌 I/O 大会上,谷歌为我们带来了智能穿戴系统 Android Wear,LG G Watch 以及三星 Gear Live 将率先支持 Android Wear.摩托罗拉发布了一段 ...

  4. Android Wear开发 - 数据通讯 - 第二节 : 数据的发送与接收

    本节由介绍3种数据的发送接收: 1.Data Items : 比特类型数据,限制100KB以内 2.Assets : 资源类型数据,大小无上限 3.Message : 发送消息,触发指令 http:/ ...

  5. 三星r381android+wear,三星新路线图:Android Wear手表/Tizen手机

    近日三星产品策略高级副总Hankil Yoon向我们介绍了三星2014年产品路线图,和2013年差不多,新的一年三星将沿着智能手表和非安卓手机两个方向发展.值得一提的是三星将会在今年推出一款高配的智能 ...

  6. Android wear

    Android wear 转载于:https://www.cnblogs.com/zhujiabin/p/5674711.html

  7. Android Wear开发者预览版入门

    Android Wear开发者预览版包括工具和API,增强了应用程序通知功能,提供针对Android可穿戴设备的最佳用户体验. 利用Android Wear开发者预览,你可以: 在Android模拟器 ...

  8. android wear 2.0 moto360 二代,最好看的 Android Wear, 二代 Moto 360 可能就是这样了

    原标题:最好看的 Android Wear, 二代 Moto 360 可能就是这样了 虽然没有人不认为 Moto 360 是最漂亮的智能手表之一,但第一代产品卖的并没有那么好.时至今日,已经到了网上开 ...

  9. android wear ios 连接,教你如何让Android Wear智能手表兼容iOS系统

    也许未来Android Wear系统智能手表有可能官方兼容iOS系统,但是想要等到这一天,似乎还得有点耐心.因此如果你恰好是一位iPhone用户,同时又不太喜欢Apple Watch,购买了一款And ...

  10. Android Wear 唤醒热词会比“你好,安卓”好吗?

    随着Motorola 在发布搭载了Android Wear 操作系统的智能手表 Moto 360 二代中国版手表,Android Wear正式进入中国大陆,因为一些原因,其最核心的语音搜索功能破天荒地 ...

最新文章

  1. 【从零开始的ROS四轴机械臂控制】(一)- 实际模型制作、Solidworks文件转urdf与rviz仿真
  2. python-匿名函数lambda
  3. Java迭代器Iterator接口
  4. vs2008 c++ 调用java
  5. nginx实现大小写字母转换(ngx_http_lower_upper_case模块)
  6. CPUID详解[增加TLB与Cache]
  7. LINUX NTP 服务器搭建
  8. html网页跳转触发器,trigger button
  9. easyui 扩展验证
  10. 解决tomcat启动startup.bat一闪而…
  11. ajax load html页面,jQuery – AJAX load() 方法 | 菜鸟教程
  12. js中如何判断按钮是否被点击了
  13. Flex皮肤制作教程
  14. 本人账户登录计算机黑屏,电脑开机输入系统密码后就黑屏了,怎么办
  15. hadoop、spark、hive、solr、es与YDB在车辆即席分析上的对比分析
  16. MTK G-sensor
  17. IT民工系列 —— 前言
  18. “追梦五年”主题征文『51CTO五周年庆』
  19. 【办公协作软件】万彩办公大师教程丨图片OCR工具的应用
  20. 名帖49 王羲之 小楷《黄庭经》

热门文章

  1. 使用gdb进行调试入门篇
  2. 傅里叶变换的终极解释下
  3. IPC生产者与消费者模型加线程
  4. 软工实践第八次作业(软件工程实践总结)
  5. expect免互交 常用编辑文本
  6. ng的概念层次(官方文档摘录)
  7. VC双缓冲画图技术介绍
  8. 浅谈内联元素inline
  9. BootStrap--CSS组件
  10. volley 框架的使用