调用手机的方向传感器,X轴,Y轴,Z轴的数值
activity_sensor.xml
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns: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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F8BBD0"
android:gravity="center">
<TextView
android:text="倾斜角(X轴):"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tvX" tools:ignore="HardcodedText"/>
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#B2DFDB">
<TextView
android:text="滚动角(Y轴):"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tvY" tools:ignore="HardcodedText,SpUsage"/>
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#B3E5FC">
<TextView
android:text="方位角(Z轴):"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tvZ" tools:ignore="HardcodedText,SpUsage"/>
<TextView
android: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>
<Button
android: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
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;
@Override
protected 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);
}
@Override
public 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);
}
@Override
public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {
}
@Override
protected void onDestroy() {
super.onDestroy();
sManager.unregisterListener(this);
}
}