博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android实现传感器应用及位置服务
阅读量:4690 次
发布时间:2019-06-09

本文共 4080 字,大约阅读时间需要 13 分钟。

Android实现传感器应用及位置服务

开发工具:Andorid Studio 1.3

运行环境:Android 4.4 KitKat

代码实现

这里需用获取加速度传感器和地磁传感器,手机获取旋转的方向都是通过加速度传感器和地磁传感器共同计算得出来的,给传感器注册监听器,这里由于精度要求,需要使用SENSOR_DELAY_GAME来提高传感器更新速率

private void initSensor() {    sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);    Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);    Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    sensorManager.registerListener(sensorEventListener, magneticSensor, SensorManager.SENSOR_DELAY_GAME);    sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME);}

在监听器里面的主要工作就是通过两者的数据共同计算旋转角度,通过矩阵计算出所需旋转的角度,更新图片旋转角度

private SensorEventListener sensorEventListener  = new SensorEventListener() {    float[] accelerometerValues = new float[3];    float[] magneticValues = new float[3];    private float lastRotateDegree;    @Override    public void onSensorChanged(SensorEvent sensorEvent) {        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {            accelerometerValues = sensorEvent.values.clone();        } else if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {            magneticValues = sensorEvent.values.clone();        }        float[] R = new float[9];        float[] values = new float[3];        SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);        SensorManager.getOrientation(R, values);        float rotateDegree = -(float)Math.toDegrees(values[0]);        if (Math.abs(rotateDegree - lastRotateDegree) > 1) {            RotateAnimation animation = new RotateAnimation(lastRotateDegree, rotateDegree,                    Animation.RELATIVE_TO_SELF, 0.5f,                    Animation.RELATIVE_TO_SELF, 0.5f);            animation.setFillAfter(true);            imgCompass.startAnimation(animation);            lastRotateDegree = rotateDegree;        }    }    @Override    public void onAccuracyChanged(Sensor sensor, int accuracy) {}};

位置服务这边通过LocationManager来获取位置服务,检测手机是否打开位置服务,主要就是调用手机的位置服务,包括GPS和Network,从Provider中获得相应的经纬度,更新UI

private void initLocation() {    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    List
providerList = locationManager.getProviders(true); String provider = null; if (providerList.contains(LocationManager.GPS_PROVIDER)) { provider = LocationManager.GPS_PROVIDER; } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) { provider = LocationManager.NETWORK_PROVIDER; } else { Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show(); return; } try { Location location = locationManager.getLastKnownLocation(provider); if (location != null) { showLocation(location); } locationManager.requestLocationUpdates(provider, 5000, 1, locationListener); } catch (Exception e) { e.printStackTrace(); }}

注册的locationListener中只需要使用onLocationChanged即可,就是更新UI

private LocationListener locationListener = new LocationListener() {    @Override    public void onLocationChanged(Location location) { showLocation(location); }    @Override    public void onStatusChanged(String s, int i, Bundle bundle) {}    @Override    public void onProviderEnabled(String s) {}    @Override    public void onProviderDisabled(String s) {}};private void showLocation(Location location) {    edtLott.setText("" + location.getLongitude());    edtLatt.setText("" + location.getLatitude());}

当程序退出的时候,需要注销已经注册的监听,关于Location的部分使用try语句,因为有可能需用使用到Network

@Overridepublic void onDestroy() {    super.onDestroy();    if (sensorManager != null) {        sensorManager.unregisterListener(sensorEventListener);    }    if (locationListener != null) {        try {            locationManager.removeUpdates(locationListener);        } catch (Exception e) {            e.printStackTrace();        }    }}

最后给程序添加使用传感器的权限即可

效果图

初始化界面->偏转手机->点击按钮->刷出经纬度

cant showcant showcant showcant show

工程下载

传送门:

转载于:https://www.cnblogs.com/wsine/p/5177885.html

你可能感兴趣的文章
C#垃圾回收机制
查看>>
31、任务三十一——表单联动
查看>>
python之hasattr、getattr和setattr函数
查看>>
maven使用阿里镜像配置文件
查看>>
Copy code from eclipse to word, save syntax.
查看>>
arguments.callee的作用及替换方案
查看>>
23 Java学习之RandomAccessFile
查看>>
P2709 小B的询问
查看>>
PHP echo 和 print 语句
查看>>
第一讲 一个简单的Qt程序分析
查看>>
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
UML-画类图与交互图的顺序
查看>>
mysql 基本操作
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
HTC Sensation G14开盒
查看>>
lock_sga引起的ksvcreate :process(m000) creation failed
查看>>
数据库插入数据乱码问题
查看>>