距離傳感器的使用


    public static void proximityStart() {
        if (VPVoicePlus.isVoicePlusOpen == false || TMAppStatusUtil.isAppOnForeground(VPVoicePlus.application) == false) {
            return;
        }
        if (VPVoicePlus.application == null) {
            return;
        }
        mSensorManager = (SensorManager) VPVoicePlus.application.getSystemService(Context.SENSOR_SERVICE);
        mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

        sensorEventListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                if (VPVoicePlus.isVoicePlusOpen == false || TMAppStatusUtil.isAppOnForeground(VPVoicePlus.application) == false) {
                    return;
                }

                if (event == null) {
                    return;
                }
                float[] values = event.values;
                if (values == null || values.length <= 0) {
                    return;
                }
                float distance = values[0];

                if (distance == 0.0) {
                    if (waitApproach) {
                        Vibrator vibrator = (Vibrator) VPVoicePlus.application.getSystemService(Service.VIBRATOR_SERVICE);
                        vibrator.vibrate(30);

                        Log.d("logvoiceplus proximity", "靠近");
                        VPVoiceDetector.setAllowVoiceOutPut(true);
                        VPVoiceDetector.voiceStart();
                        VPVoicePlus.addUserTrack(VPVoicePlus.UserTrackState.VOICE_DETECT_START);
                        detectStartTime = System.currentTimeMillis();
                        waitApproach = false;
                    }

                    Message msg = timeHandler.obtainMessage();
                    msg.what = MSG_TIME;
                    timeHandler.sendMessageDelayed(msg, delayTime);
                }
                if (distance > 0.0) {
                    Log.d("logvoiceplus proximity", "遠離");
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - detectStartTime < 500) {
                        Log.d("logvoiceplus proximity", "時間太短不輸出");
                        VPVoiceDetector.setAllowVoiceOutPut(false);
                        VPVoiceDetector.voiceStop();
                    }
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };

        Handler mainHandler = new Handler(Looper.getMainLooper());
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mSensorManager != null) {
                    mSensorManager.registerListener(sensorEventListener, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
                }
            }
        });
    }

    public static void proximityStop() {
        if (mSensorManager != null) {
            mSensorManager.unregisterListener(sensorEventListener, mProximity);
            Log.d("logvoiceplus proximity", "unregister");
        }
    }

注意android手機差別很大,靠近的時候,所有的手機的distance返回的都是0,但是再遠離的時候,有些手機反回5.0,有些是8.0。。。,所以只能用>0來判斷遠離

發佈了74 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章