为什么运动传感器不触发?

时间:2019-03-16 08:55:57

标签: android

我正在尝试使用以下代码测试我的运动传感器(在Galaxy S7上):

 public class MainActivity extends AppCompatActivity {

        private SensorManager sensorManager;
        private Sensor sensorGRAVITY;
        private Sensor sensorACCELEROMETER;
        private TriggerEventListener triggerEventListenerGRAVITY;
        private TriggerEventListener triggerEventListenerACCELEROMETER;



        private void initManagers() {
            sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

            // --- TYPE_GRAVITY
            sensorGRAVITY = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
            triggerEventListenerGRAVITY = new TriggerEventListener() {
                @Override
                public void onTrigger(TriggerEvent event) {
                    TextView textView = (TextView)findViewById(R.id.textView1);
                    textView.setText("[0] = " + String.valueOf(event.values[0]) + " [1] = " + String.valueOf(event.values[1]));
                }
            };
            sensorManager.requestTriggerSensor(triggerEventListenerGRAVITY, sensorGRAVITY);

            // --- TYPE_ACCELEROMETER
            sensorACCELEROMETER = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            triggerEventListenerACCELEROMETER = new TriggerEventListener() {
                @Override
                public void onTrigger(TriggerEvent event) {
                    TextView textView = (TextView)findViewById(R.id.textView2);
                    textView.setText("[0] = " + String.valueOf(event.values[0]) + " [1] = " + String.valueOf(event.values[1]));
                }
            };
            sensorManager.requestTriggerSensor(triggerEventListenerACCELEROMETER, sensorACCELEROMETER);
        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initManagers();

        }


    }

,但似乎没有触发器显示。 我试图交谈并移动我的Galaxy几秒钟,并且textview1和textview2都没有改变。

我想念什么?

1 个答案:

答案 0 :(得分:0)

尝试在initmanagers()方法之外声明TriggerEventListener()。 私有无效initManagers(){

       sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

            // --- TYPE_GRAVITY
            sensorGRAVITY = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
            triggerEventListenerGRAVITY = new TriggerEventListener() {
                @Override
                public void onTrigger(TriggerEvent event) {
                    TextView textView = (TextView)findViewById(R.id.textView1);
                    textView.setText("[0] = " + String.valueOf(event.values[0]) + " [1] = " + String.valueOf(event.values[1]));
                }
            };
            sensorManager.requestTriggerSensor(triggerEventListenerGRAVITY, sensorGRAVITY);

            // --- TYPE_ACCELEROMETER
            sensorACCELEROMETER = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

            sensorManager.requestTriggerSensor(triggerEventListenerACCELEROMETER, sensorACCELEROMETER);
        }
triggerEventListenerACCELEROMETER = new TriggerEventListener() {
                @Override
                public void onTrigger(TriggerEvent event) {
                    TextView textView = (TextView)findViewById(R.id.textView2);
                    textView.setText("[0] = " + String.valueOf(event.values[0]) + " [1] = " + String.valueOf(event.values[1]));
                }
            };