就是把手机里面多有的传感器数据获取到,然后显示在界面上?求大神解决。
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
方向传感器: Sensor.TYPE_ORIENTATION加速度传感器: Sensor.TYPE_ACCELEROMETER光线传感器: Sensor.TYPE_LIGHT磁场传感器: Sensor.TYPE_MAGNETIC_FIELD距离传感器: Sensor.TYPE_PROXIMITY温度传感器: Sensor.TYPE_TEMPERATURE
//获取某种类型的感应器Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//注册监听sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
上面第三个参数为采样率最快: SensorManager.SENSOR_DELAY_FASTEST游戏: SensorManager.SENSOR_DELAY_GAME普通: SensorManager.SENSOR_DELAY_NORMAL用户界面: SensorManager.SENSOR_DELAY_UI
用获取加速传感器数据举例:
//MainActivity.java package aaa.android.com.myapplication123; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView; private MySensorEventListener sensorEventListener = new MySensorEventListener(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text_view); //获取管理器 SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); //获取加速度感应器 Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL); } private final class MySensorEventListener implements SensorEventListener { //传感器实时变化值 @Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; String result = "X= " + x + "\n" + "Y= " + y + "\n" + "Z= " + z; textView.setText(result); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } } //activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="30dp" tools:context="aaa.android.com.myapplication123.MainActivity"> <!--定义一个textview用来显示结果--> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="test" android:textSize="30sp" android:id="@+id/text_view"/> </LinearLayout>
方向传感器: Sensor.TYPE_ORIENTATION
加速度传感器: Sensor.TYPE_ACCELEROMETER
光线传感器: Sensor.TYPE_LIGHT
磁场传感器: Sensor.TYPE_MAGNETIC_FIELD
距离传感器: Sensor.TYPE_PROXIMITY
温度传感器: Sensor.TYPE_TEMPERATURE
//获取某种类型的感应器
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//注册监听
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
上面第三个参数为采样率
最快: SensorManager.SENSOR_DELAY_FASTEST
游戏: SensorManager.SENSOR_DELAY_GAME
普通: SensorManager.SENSOR_DELAY_NORMAL
用户界面: SensorManager.SENSOR_DELAY_UI
用获取加速传感器数据举例: