调用手机的方向传感器,X轴,Y轴,Z轴的数值
activity_sensor.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"tools:context="com.weijun901.randomNum.Second"><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center" tools:ignore="MissingConstraints"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#F8BBD0"android:gravity="center"><TextViewandroid:text="倾斜角(X轴):"android:textSize="30sp"android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/tvX" tools:ignore="HardcodedText"/><TextViewandroid:text="TextView"android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/tv1" android:layout_weight="1"tools:ignore="HardcodedText,InefficientWeight"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#B2DFDB"><TextViewandroid:text="滚动角(Y轴):"android:textSize="30dp"android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/tvY" tools:ignore="HardcodedText,SpUsage"/><TextViewandroid:text="TextView"android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/tv2" android:layout_weight="1"tools:ignore="HardcodedText,InefficientWeight"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#B3E5FC"><TextViewandroid:text="方位角(Z轴):"android:textSize="30dp"android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/tvZ" tools:ignore="HardcodedText,SpUsage"/><TextViewandroid:text="TextView"android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/tv3" android:layout_weight="1"tools:ignore="HardcodedText,InefficientWeight"/></LinearLayout><Buttonandroid:text="Main"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="toMainActivity"android:id="@+id/button" tools:ignore="HardcodedText"/></LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Sensor.java
package com.weijun901.show;import android.content.Intent;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;public class Sensor extends AppCompatActivity implements SensorEventListener {private TextView tv1;private TextView tv2;private TextView tv3;private SensorManager sManager;private android.hardware.Sensor mSensorOrientation;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sensor);// 设置标题栏的文字getSupportActionBar().setTitle("方向传感器");sManager = (SensorManager) getSystemService(SENSOR_SERVICE);mSensorOrientation = sManager.getDefaultSensor(android.hardware.Sensor.TYPE_ORIENTATION);sManager.registerListener(this, mSensorOrientation, SensorManager.SENSOR_DELAY_UI);bindViews();}private void bindViews() {tv1 = findViewById(R.id.tv1);tv2 = findViewById(R.id.tv2);tv3 = findViewById(R.id.tv3);}@Overridepublic void onSensorChanged(SensorEvent event) {tv1.setText((float) (Math.round(event.values[1] * 100)) / 100 + "°");tv2.setText((float) (Math.round(event.values[2] * 100)) / 100 + "°");tv3.setText((float) (Math.round(event.values[0] * 100)) / 100 + "°");}public void toMainActivity(View view) {Intent intent = new Intent(this, MainActivity.class); // 替换为目标页面的类名startActivity(intent);}@Overridepublic void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {}@Overrideprotected void onDestroy() {super.onDestroy();sManager.unregisterListener(this);}
}