Android之使用传感器获取相应数据

Android的大部分手机中都有传感器,传感器类型有方向、加速度(重力)、光线、磁场、距离(临近性)、温度等。

  方向传感器:   Sensor.TYPE_ORIENTATION

  加速度(重力)传感器: Sensor.TYPE_ACCELEROMETER

  光线传感器:    Sensor.TYPE_LIGHT

  磁场传感器:   Sensor.TYPE_MAGNETIC_FIELD

  距离(临近性)传感器: Sensor.TYPE_PROXIMITY

  温度传感器:   Sensor.TYPE_TEMPERATURE

//获取某种类型的感应器

Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

//注册监听,获取传感器变化值

sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);

扫描二维码关注公众号,回复: 1471849 查看本文章

上面第三个参数为采样率:最快、游戏、普通、用户界面。当应用程序请求特定的采样率时,其实只是对传感器子系统的一个建议,不保证特定的采样率可用。

最快: SensorManager.SENSOR_DELAY_FASTEST

最低延迟,一般不是特别敏感的处理不推荐使用,该种模式可能造成手机电力大量消耗,由于传递的为原始数据,算法不处理好将会影响游戏逻辑和UI的性能。

游戏: SensorManager.SENSOR_DELAY_GAME

游戏延迟,一般绝大多数的实时性较高的游戏都使用该级别。

普通: SensorManager.SENSOR_DELAY_NORMAL

标准延迟,对于一般的益智类或EASY级别的游戏可以使用,但过低的采样率可能对一些赛车类游戏有跳帧现象。

用户界面: SensorManager.SENSOR_DELAY_UI

一般对于屏幕方向自动旋转使用,相对节省电能和逻辑处理,一般游戏开发中我们不使用。

使用传感器做应用的难点在于获取数据后如何处理,获得相应需要的效果,这里涉及很多数学物理的知识……

下面使用一个代码实例演示如何获取方向和磁场强度的数据:

package com.magnetic;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView magneticView;    
    private TextView orientationView;    
    private TextView totalMageticView;
    private SensorManager sensorManager;    
    private MySensorEventListener sensorEventListener;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        sensorEventListener = new MySensorEventListener();    
        magneticView = (TextView) this.findViewById(R.id.magneticView);    
        orientationView = (TextView) this.findViewById(R.id.orientationView);  
        totalMageticView=(TextView) this.findViewById(R.id.totalMageticView);
        //获取感应器管理器    
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);   
    }
    @Override    
    protected void onResume()     
    {    
        //获取方向传感器    
        @SuppressWarnings("deprecation")
        Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);    
        sensorManager.registerListener(sensorEventListener, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);    
            
        //获取加速度传感器    
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);    
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);    
        super.onResume();    
    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private final class MySensorEventListener implements SensorEventListener    
    {    
        //可以得到传感器实时测量出来的变化值    
        @Override    
        public void onSensorChanged(SensorEvent event)     
        {       
            //得到方向的值    
            if(event.sensor.getType()==Sensor.TYPE_ORIENTATION)    
            {    
                float x = event.values[SensorManager.DATA_X];          
                float y = event.values[SensorManager.DATA_Y];          
                float z = event.values[SensorManager.DATA_Z];      
                orientationView.setText("方向: " + x + ", " + y + ", " + z);     
            }    
            //得到加速度的值    
            else if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD)    
            {    
                float x = event.values[SensorManager.DATA_X];          
                float y = event.values[SensorManager.DATA_Y];          
                float z = event.values[SensorManager.DATA_Z];     
                magneticView.setText("合磁场强度X、Y、Z分量: " + x + ", " + y + ", " + z);   
                //计算和磁场强度
                float total=(float)(Math.sqrt(Math.pow(x,2)+Math.pow(y,2)+Math.pow(z, 2)));
                totalMageticView.setText("合磁场强度: " + total); 
            }  
        }    
        //重写变化    
        @Override    
        public void onAccuracyChanged(Sensor sensor, int accuracy)     
        {    
        }    
    }   
    
    //暂停传感器的捕获    
    @Override    
    protected void onPause()     
    {    
        sensorManager.unregisterListener(sensorEventListener);    
        super.onPause();    
    }        
    
}
<?xml version="1.0" encoding="utf-8"?>
<TableLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <TableRow>
    <TextView 
        android:id="@+id/orientationView"
        android:text="方向传感器:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
    />    
    </TableRow>    
    <TableRow>
    <TextView 
        android:id="@+id/magneticView"
        android:text="磁传感器:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
    />    
    </TableRow>
    <TableRow>
    <TextView 
        android:id="@+id/totalMageticView"
        android:text="合磁强度:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
    />    
    </TableRow>
</TableLayout>

猜你喜欢

转载自www.cnblogs.com/zhaoyanhaoBlog/p/9139563.html
今日推荐