距离传感器的使用


    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来判断远离

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值