Android传感器有以下几种:
加速度传感器(accelerometer)
陀螺仪传感器(gyroscope)
环境光照传感器(light)
磁力传感器(magnetic field)
方向传感器(orientation)
压力传感器(pressure)
距离传感器(proximity)
温度传感器(temperature)
使用方法大体类似,都是先获取SensorMannager,然后实现SensorEventListener接口,再注册SensorEventListener,接口中有Senser获取到的数据的回调。
以下实现一个加速度传感器(accelerometer)的使用方法。
1 Activity中代码
public class MainActivity extends AppCompatActivity {
private Sensor graveSensor;
private SensorManager sensorManager;
private ViewDataBinding viewDataBinding;
private MainViewModel mainViewModel;
//保存 x y z 的坐标
float bx = 0; float by = 0; float bz = 0;
long bTime = 0;//这一次的时间
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mainViewModel = new MainViewModel();
viewDataBinding.setVariable(com.senser.BR.model, mainViewModel);
//1获取SensorMannager
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null) {
List<Sensor> graveSensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
graveSensor = graveSensors.get(0);
}
}
//2实现SensorEventListener接口
private SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {//S4enser获取到的数据
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
long time=System.currentTimeMillis()-bTime;
float speadX = (x - bx) / ( time);//X轴的速度
float speadY = (y - by) / (time); //y轴的速度
float speadZ = (z - bz) / (time); //z轴的速度
mainViewModel.setX(speadX);
mainViewModel.setY(speadY);
mainViewModel.setZ(speadZ);
bx = x; by = y; bz = z;
bTime = System.currentTimeMillis();
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
@Override
protected void onResume() {
super.onResume();
//注册SensorEventListener
sensorManager.registerListener(listener, graveSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onDestroy() {
super.onDestroy();
sensorManager.unregisterListener(listener);
}
}
2 viewModel中代码
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import com.senser.BR;
/**
* Created by c9736 on 2017/7/10.
*/
public class MainViewModel extends BaseObservable {
private float x, y, z;
@Bindable
public String getX() {
return "x="+x;
}
public void setX(float x) {
this.x = x;
notifyPropertyChanged(BR.x);
}
@Bindable
public String getY() {
return "y="+y;
}
public void setY(float y) {
this.y = y;
notifyPropertyChanged(BR.y);
}
@Bindable
public String getZ() {
return "z="+z;
}
public void setZ(float z) {
this.z = z;
notifyPropertyChanged(BR.z);
}
}
3布局文件
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="model"
type="com.senser.MainViewModel" />
</data>
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.senser.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{model.x}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{model.y}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{model.z}" />
</LinearLayout>
</layout>