c***@qq.com
c***@qq.com
  • 发布:2019-04-09 17:13
  • 更新:2019-11-08 11:07
  • 阅读:1266

native.js不能获取传感器信息

分类:Native.js

native.js不能获取传感器信息,代码如下:

<script type="text/javascript">  
            document.addEventListener('plusready', function() {  
                //console.log("所有plus api都应该在此事件发生后调用,否则会出现plus is undefined。"  
                plus.android.importClass('java.util.List');  
                plus.android.importClass('java.util.ArrayList');  
                var SensorEventListener = plus.android.importClass('android.hardware.SensorEventListener');  
                var Context = plus.android.importClass("android.content.Context");  
                var mContext = plus.android.runtimeMainActivity();  
                var SensorManager = plus.android.importClass("android.hardware.SensorManager");  
                var hevent = plus.android.implements("android.hardware.SensorEventListener", {  
                    onSensorChanged: function(event) {  
                        console.log(event.values);  
                    }  
                });  

                var Sensor = plus.android.importClass("android.hardware.Sensor");  
                var mSensorManager = mContext.getSystemService(Context.SENSOR_SERVICE);  
                var mList = mSensorManager.getSensorList(Sensor.TYPE_ALL);  
                let sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
                mSensorManager.registerListener(hevent, sensor, SensorManager.SENSOR_DELAY_NORMAL);  
            });  
        </script>
2019-04-09 17:13 负责人:无 分享
已邀请:
4***@qq.com

4***@qq.com

document.addEventListener('plusready', function() {  
                //console.log("所有plus api都应该在此事件发生后调用,否则会出现plus is undefined。"  
                var activity = plus.android.runtimeMainActivity();  
                var Sensor = plus.android.importClass("android.hardware.Sensor");  
                var SensorManager = plus.android.importClass("android.hardware.SensorManager");  
                var Context = plus.android.importClass("android.content.Context");  

                var mSensorManager = activity.getSystemService(Context.SENSOR_SERVICE);  

                var mStepCount = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);  
                // var mStepDetector = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);  

                var listener = plus.android.implements("android.hardware.SensorEventListener", {  
                    onAccuracyChanged: function(sensor, accuracy){  

                    },  
                    onSensorChanged: function(event) {  
                        console.log("传感器类型:"+event.plusGetAttribute("sensor").getType());  
                        console.log("当前步数总计:"+event.plusGetAttribute("values"));  
                    }  
                });  
                mSensorManager.unregisterListener(listener);  

                // mSensorManager.registerListener(listener, mStepDetector, SensorManager.SENSOR_DELAY_FASTEST);  
                mSensorManager.registerListener(listener, mStepCount, SensorManager.SENSOR_DELAY_FASTEST);  
            });

该问题目前已经被锁定, 无法添加新回复