Android Wear Hello World

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 Getting Started 1

Android Wear Hello World入门1

Android Wear Hello World Getting Started 2

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 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" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <uses-library
            android: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-data
            android:name="com.google.android.wearable.standalone"
            android:value="true" />

        <activity
            android: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.BoxInsetLayout
    xmlns: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">



    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/inner_frame_layout_padding"
        app:boxedEdges="none">


        <android.support.wear.widget.SwipeDismissFrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/swipe_dismiss_root" >

            <TextView
                android: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>


        <TextView
            android: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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = findViewById(R.id.text);

        // Enables Always-on
        setAmbientEnabled();
    }
}

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 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存储库中查看完整的源代码。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Wear车钥匙的源码是一种应用程序代码,它用于开发与Android Wear设备兼容的汽车钥匙应用程序。这些应用程序允许用户通过他们的Android Wear设备来控制和管理他们的汽车。 源码包含了应用程序的各个部分,包括用户界面、功能模块和与汽车通信的代码。它们通过使用Java编程语言以及特定于Android Wear的API和框架来实现。 其中的用户界面部分通常包括展示汽车信息和控制功能的屏幕。这些屏幕可以显示车辆的基本信息,比如速度、油耗和剩余里程等。此外,它们还可以提供启动和停止引擎、解锁和锁定车门、调整温度和播放音乐等功能。 功能模块是源码的另一个重要组成部分。它们负责将用户的操作转化为与汽车通信的指令。这些指令通过与汽车的关键系统(如引擎管理系统、车门锁定系统等)通信来实现各种功能。 最后,与汽车通信的代码允许Android Wear设备与汽车之间进行数据交换。这些代码通常使用蓝牙或Wi-Fi等通信技术来连接Android Wear设备和汽车,以便实现双向通信和数据传输。 总的来说,Android Wear车钥匙的源码是一套为开发汽车钥匙应用程序而设计的代码,它包含用户界面、功能模块和与汽车通信的代码。开发人员可以使用这些源码来创建自定义的汽车钥匙应用程序,并为用户提供方便和实用的汽车控制功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值