private TextView name;
private void initView() {
name = (TextView) findViewById(R.id.name);
}
li = new ArrayList<>();
li.add("DDDDD");
li.add("WWWWW");
li.add("XXXXX");
private void initData() {
SensorManager service = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
service.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();//传感器类型
float[] values = event.values;
if (type == Sensor.TYPE_ACCELEROMETER) {
if (Math.abs(values[0]) > 12 || Math.abs(values[1]) > 12 || Math.abs(values[2]) > 12) {
a = (int) (Math.random() * (li.size()));
name.setText(li.get(a));
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, service.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}