距离传感器的使用
来源:互联网 发布: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
- 距离传感器的使用
- (九十一)距离传感器的使用
- 29.iPhone距离传感器的简单使用
- android距离传感器的应用
- 距离传感器
- 距离传感器
- 距离传感器
- ios开发之距离传感器使用
- 实用知识:距离传感器方法使用
- 传感器的使用:光线传感器
- 传感器的使用:加速度传感器
- 传感器的使用:方向传感器
- 加速度、陀螺仪、电源、距离传感器的使用方法
- 没有中断的距离传感器,打电话灭屏后无法使用PS亮屏
- Android传感器之距离传感器
- android 传感器的使用
- android传感器的使用
- 传感器的使用
- Java 泛型类
- Android DrawableUtil 工具类,
- Android Studio 修改项目包名(任意级)
- c++作业-3
- Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net
- 距离传感器的使用
- 【hdu1711】Number Sequence——KMP
- 使用springmvc遇到的问题
- C语言复习
- HLJCORS运行中心建设进入设备安装调试阶段
- 深入理解Java内存模型
- leetcode 71. Simplify Path
- minAreaRect-OpenCV
- How to Setup network on centos 7