距离传感器的使用

来源:互联网 发布:ubuntu 设置交换分区 编辑:程序博客网 时间:2024/05/08 10:16
    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来判断远离

0 0
原创粉丝点击