xml Android Wear:Heart Rate和Samsung Gear Live。 (基本例子)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml Android Wear:Heart Rate和Samsung Gear Live。 (基本例子)相关的知识,希望对你有一定的参考价值。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
tools:deviceIds="wear_round">
<android.support.wearable.view.CircledImageView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
app:circle_radius="50dp"
android:id="@+id/circle"
app:circle_color="@color/light_grey"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="center"
android:text="wait...."
android:textColor="@color/black"
android:textSize="20sp"
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.wearable.view.CircledImageView>
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect_heart"
app:roundLayout="@layout/round_heart"
tools:context=".MyActivity"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>
public class MyActivity extends Activity implements SensorEventListener {
//UI Elements
private CircledImageView mCircledImageView;
private TextView mTextView;
//Sensor and SensorManager
Sensor mHeartRateSensor;
SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.heart_layout);
//Sensor and sensor manager
mSensorManager = ((SensorManager)getSystemService(SENSOR_SERVICE));
mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
//View
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mCircledImageView = (CircledImageView) stub.findViewById(R.id.circle);
mTextView = (TextView) stub.findViewById(R.id.value);
}
});
}
@Override
protected void onResume() {
super.onResume();
//Register the listener
if (mSensorManager != null){
mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onPause() {
super.onPause();
//Unregister the listener
if (mSensorManager!=null)
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
//Update your data. This check is very raw. You should improve it when the sensor is unable to calculate the heart rate
if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
if ((int)event.values[0]>0) {
mCircledImageView.setCircleColor(getResources().getColor(R.color.green));
mTextView.setText("" + (int) event.values[0]);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
<!-- Declare the permission for body sensor -->
<uses-permission android:name="android.permission.BODY_SENSORS" />
以上是关于xml Android Wear:Heart Rate和Samsung Gear Live。 (基本例子)的主要内容,如果未能解决你的问题,请参考以下文章
在openGl android(Wear)中绘制基于图像的纹理
Android Wear 布局开始太低
圆形 Android Wear 设计的 Snackbar
Xamarin.Forms - 创建 Android Wear 应用程序时出现问题
在 Android Wear 中正确显示通知
Android Wear OS 关闭 Wifi