Android Wear 进阶 2.1 Creating and Running a Wearable App 创建和运行手表应用

Creating and Running a Wearable App

Wearable apps run directly on the wearable device, giving you access to low-level hardware such as sensors, activities, services, and more, right on the wearable.

手表应用直接运行在手表设备上面,给你访问底层的硬件,例如感应器,activities,服务。

A companion handheld app that contains the wearable app is also required when you want to publish to the Google Play store. Wearables don't support the Google Play store, so users download the companion handheld app, which automatically pushes the wearable app to the wearable. The handheld app is also useful for doing heavy processing, network actions, or other work and sending the results to the wearable.

This lesson goes over how to set up a device or emulator and create one project to contain both your wearable and handheld apps.

相应的手机应用包含了手表应用在你把应用发布到Google Play store 的时候是必须的。手表不支持Google Play store,所以用户下载相应地手机应用,这个应用会自动的将手表应用推送到手表端。手表应用同时也对于处理重的进程,例如网络的请求action,或者其他工作,并且把结果发送到手表端。

这个课程将了如何设置一个设备或者模拟器,并且创建一个包含了手表和手机应用的项目。

Update Your SDK 更新你的SDK



Before you begin building wearable apps, you must:

在你开始创建手表应用之前,你必须保证:

  • Update your SDK tools to version 23.0.0 or higher 
    The updated SDK tools enable you to build and test wearable apps.
  • Update your SDK with Android 4.4W.2 (API 20) or higher 
    The updated platform version provides new APIs for wearable apps.

To update your SDK with these components, see Get the latest SDK tools.

Set Up an Android Wear Emulator or Device



We recommend that you develop on real hardware so you can better gauge the user experience. However, the emulator lets you test out different types of screen shapes, which is useful for testing.

Set up an Android Wear Virtual Device

To set up an Android Wear virtual device:

  1. Click Tools > Android > AVD Manager.
  2. Click Create Virtual Device....
    1. Click Wear in the Category list:
    2. Select Android Wear Square or Android Wear Round.
    3. Click Next.
    4. Select a release name (for example, KitKat Wear).
    5. Click Next.
    6. (Optional) Change any preferences for your virtual device.
    7. Click Finish.
  3. Start the emulator:
    1. Select the virtual device you just created.
    2. Click the Play button.
    3. Wait until the emulator initializes and shows the Android Wear home screen.
  4. Pair your handheld with the emulator:
    1. On your handheld, install the Android Wear app from Google Play.
    2. Connect the handheld to your machine through USB.
    3. Forward the AVD's communication port to the connected handheld device (you must do this every time the handheld is connected):
      adb -d forward tcp:5601 tcp:5601
    4. Start the Android Wear app on your handheld device and connect to the emulator.
    5. Tap the menu on the top right corner of the Android Wear app and select Demo Cards.
    6. The cards you select appear as notifications on the home screen of the emulator.

Set Up an Android Wear Device

To set up an Android Wear device:

  1. Install the Android Wear app, available on Google Play, on your handheld.
  2. Follow the app's instructions to pair your handheld with your wearable. This allows you to test out synced handheld notifications, if you're building them.
  3. Leave the Android Wear app open on your phone.
  4. Enable adb debugging on the Android Wear device.
    1. Go to Settings > About.
    2. Tap Build number seven times.
    3. Swipe right to return to the Settings menu.
    4. Go to Developer options at the bottom of the screen.
    5. Tap ADB Debugging to enable adb.
  5. Connect the wearable to your machine through USB, so you can install apps directly to it as you develop. A message appears on both the wearable and the Android Wear app prompting you to allow debugging.

    Note: If you can not connect your wearable to your machine via USB, you can try connecting over Bluetooth.

  6. On the Android Wear app, check Always allow from this computer and tap OK.

The Android tool window on Android Studio shows the system log from the wearable. The wearable should also be listed when you run the adb devices command.

Create a Project



To begin development, create an app project that contains wearable and handheld app modules. In Android Studio, click File > New Project and follow the Project Wizard instructions, as described in Creating a Project. As you follow the wizard, enter the following information:

  1. In the Configure your Project window, enter a name for your app and a package name.
  2. In the Form Factors window:
    • Select Phone and Tablet and select API 9: Android 2.3 (Gingerbread) under Minimum SDK.
    • Select Wear and select API 20: Android 4.4 (KitKat Wear) under Minimum SDK.
  3. In the first Add an Activity window, add a blank activity for mobile.
  4. In the second Add an Activity window, add a blank activity for Wear.

When the wizard completes, Android Studio creates a new project with two modules, mobile and wear. You now have a project for both your handheld and wearable apps for which you can create activities, services, and custom layouts. The handheld app does most of the heavy lifting, such as network communications, intensive processing, or tasks that require long amounts of user interaction. When the app completes these operations, your app should notify the wearable of the results through notifications or by syncing and sending data to the wearable.

Note: The wear module also contains a "Hello World" activity that uses a WatchViewStub. This class inflates a layout based on whether the device's screen is round or square. The WatchViewStub class is one of the UI widgets that the wearable support library provides.

Install the Wearable App



When developing, you install apps directly to the wearable like with handheld apps. Use either adb install or the Play button on Android Studio.

When you're ready to publish your app to users, you embed the wearable app inside of the handheld app. When a user installs the handheld app from Google Play, a connected wearable automatically receives the wearable app.

Note: The automatic installation of wearable apps does not work when you are signing apps with a debug key and only works with release keys. See Packaging Wearable Apps for complete information on how to properly package wearable apps.

  • To install the "Hello World" app to the wearable, select wear from the Run/Debug configuration drop-down menu and click the Play button. The activity shows up on the wearable and prints out "Hello world!"

    Include the Correct Libraries



    As part of the Project Wizard, the correct dependencies are imported for you in the appropriate module'sbuild.gradle file. However, these dependencies are not required, so read the following descriptions to find out if you need them or not:作为项目向导的一部分,build.gradle 已经将正确的依赖导入了。但是这个依赖不是必须的,所以阅读下面的描述去确定你是否需要他们。

    Notifications 通知

    The Android v4 support library (or v13, which includes v4) contains the APIs to extend your existing notifications on handhelds to support wearables.支持包Android v4或者是v13(包含了v4) 已经包含了扩展手机上面的通知去支持手表端。

    For notifications that appear only on the wearable (meaning, they are issued by an app that runs on the wearable), you can just use the standard framework APIs (API Level 20) on the wearable and remove the support library dependency in the mobile module of your project. 对于那些只出现在手表端的通知,你可以在手表上面就使用标准的框架API(API 20),将手机上面的支持包依赖删除。

    Wearable Data Layer 手表的数据层

    To sync and send data between wearables and handhelds with the Wearable Data Layer APIs, you need the latest version of Google Play services. If you're not using these APIs, remove the dependency from both modules.为了在手表和手机端通过手表数据层的API来同步和发送数据,你需要最新的版本的Google Play service。如果你没有使用这写APIs,在手表段和手机端都一处掉依赖。

    Wearable UI support library 手表UI的支持库

    This is an unofficial library that includes UI widgets designed for wearables. We encourage you to use them in your apps, because they exemplify best practices, but they can still change at any time. However, if the libraries are updated, your apps won't break since they are compiled into your app. To get new features from an updated library, you just need to statically link the new version and update your app accordingly. This library is only applicable if you create wearable apps.这个是一个非官方的库,它包含了UI widgets designed for wearables. 我们建议你去在你的应用中使用他们,因为他们已经使用的非常好了,但是他们可能随时会变化。然后,如果库更新了,你的应用不会break因为他们会兼容你的应用的。为了从更新了的库中得到新的特性,你只需要去连接新的版本然后根据相关的去更新你的应用。这个库只有在你创建手表应用的额时候才应用。

    In the next lessons, you'll learn how to create layouts designed for wearables as well as how to use the various voice actions that are supported by the platform.在下面的额课程中,你将会学习如何去创建手表布局,同时还有怎么去使用不同的声音action ,这个action是不同支持的。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Paperback: 280 pages Publisher: Wiley; 1 edition (September 15, 2015) Language: English ISBN-10: 111905110X ISBN-13: 978-1119051107 Software Development/Mobile/Android/Wearable/Fitness Build "Wearable" Applications on the Android Wear and Google Fit Platforms This book covers wearable computing and wearable application development particularly for Android Wear (smartwatches) and Google Fit (fitness sensors). It provides relevant history, background and core concepts of wearable computing and ubiquitous computing, as a foundation for designing/developing applications for the Android Wear and Google Fit platforms. This book is intended for Android wearable enthusiasts, technologists and software developers. Gain insight into “wearables” in the modern consumer ecosystem of a multitude of devices, ubiquitous computing, cloud computing and intelligent personal assistants Learn the Android Wear and Google Fit APIs and jump-start hands-on development including: setting up an Android development environment suitable for Android Wear and Google Fit , setting up smartwatch and fitness devices for development and debugging , writing applications that install and execute on Android Wear (smartwatch) devices , and applications that run on your handheld Android devices and find and connect to fitness sensors and access fitness data, and more Catch up with the new Android 5.0 “Lollipop”, Android Studio and the gradle based build system Learn how to write applications for smart watches and fitness sensors on the Android/Google ecosystem.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值