package com.example.yanglin.myapplication;
/**
* Created by yanglin on 2018/10/7.
*/
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
public class TestSensor extends Activity implements SensorEventListener{
private SensorManager sensorManager = null;
private Sensor gyroSensor = null;
private TextView vX;
private TextView vY;
private TextView vZ;
private TextView v;
private Button button;
private static final float NS2S = 1.0f / 1000000000.0f;
private float timestamp;
private float[] angle = new float[3];
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
vX = (TextView) findViewById(R.id.vx);
vY = (TextView)findViewById(R.id.vy);
vZ = (TextView)findViewById(R.id.vz);
v = (TextView)findViewById(R.id.v);
button = (Button)findViewById(R.id.button);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
gyroSensor = sensorManager
.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
vX.setText("!!!!!!");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//声明可变字符串
StringBuffer sb = new StringBuffer();
//获取手机全部的传感器
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
//迭代输出获得上的传感器
for (Sensor sensor : sensors) {
sb.append(sensor.getName().toString());
sb.append("\n");
Log.i("传感器", sensor.getName().toString());
}
//给文本控件赋值
v.setText(sb.toString());
}
});
}
public TestSensor() {
// TODO Auto-generated constructor stub
angle[0] = 0;
angle[1] = 0;
angle[2] = 0;
timestamp = 0;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sensorManager.unregisterListener(this); // 解除监听器注册
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sensorManager.registerListener(this, gyroSensor,
SensorManager.SENSOR_DELAY_NORMAL); //为传感器注册监听器
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
showInfo("事件:" + " x:" + event.values[0] + " y:" + event.values[1] + " z:" + event.values[2]);
vX.setText("Gyroscope X: " + event.values[0]);
vY.setText("Gyroscope Y: " + event.values[1]);
vZ.setText("Gyroscope Z: " + event.values[2]);
}
}
private void showInfo(String info){
Log.e("陀螺仪",info);}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Test1"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取传感器"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/v"
android:textSize="30px"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vx"
android:textSize="50px"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vy"
android:textSize="50px"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vz"
android:textSize="50px"
/>
</LinearLayout>