Android Wear Hello World
时间:2020-02-23 14:29:24 来源:igfitidea点击:
今天,我们将讨论使用Android Studio和Wear OS模拟器的基本的hello world应用程序与Android Wear。
Android Wear应用类型
Android Wear Apps可以有两种类型:
- 独立应用
- 伴侣应用程序–具有等效的电话应用程序,可以与穿戴应用程序进行通信。
首先,在我们的Android Studio项目中创建一个简单的独立Wear OS应用。
项目结构
我们的项目结构如下所示:
Android Wear Hello World项目结构
在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
Wear OS App的列表与普通的Android Phone Apps略有不同。
其中我们需要指定功能并安装OS库以及一些元数据。
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.theitroad.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>
布局
磨损为os的智能手表最常用的布局是BoxInsetLayout。
<android.support.wearable.view.BoxInsetLayout>
...
<LinearLayout
...
app:layout_box="all">
</android.support.wearable.view.BoxInsetLayout>
另一个常用的布局是" SwipeDismissFrameLayout"。
这样可以从左向右滑动。
磨损os的RecyclerView等效类是WearableRecyclerView。
android.support.wearable.view.CircledImageView提供了一种圆形布局来显示图像。
下面给出了" 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>
其中我们添加了滑动以关闭带有文本视图的布局。
代码
MainActivity.java类的代码如下:
package com.theitroad.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();
}
}
常亮功能可让应用控制在环境模式下手表上显示的内容。

