检测手机传感器【Sensor】

      今天做了一个小Demo,主要是检测手机传个器种类个数,代码如下。

View Code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/tv_view"
        android:textStyle="italic"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />
</RelativeLayout>

 

View Code
package com.example.sensordemo;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv_view;
    private Sensor sensor;
    private float x, y, z;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void init() {
        tv_view = (TextView) this.findViewById(R.id.tv_view);
        /** 设置实例化滚动方法 */
        tv_view.setMovementMethod(ScrollingMovementMethod.getInstance());
        
        // 从系统服务中获得传感器管理器
        SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        // 从传感器管理器中获得全部的传感器列表
        List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
        // 显示有多少个传感器
        tv_view.setText("经检测手机有" + "【" + allSensors.size() + "】" + "个传感器。"
                + "\n" + "他们分别是:\n");

        // 显示每个传感器的具体信息
        for (Sensor s : allSensors) {
            String temp = "\n" + "设备名称:" + s.getName() + "\n" + "设备版本:"
                    + s.getVersion() + "\n" + "设备供应商:" + s.getVendor() + "\n";
            switch (s.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + "加速度传感器:" + temp);
                break;
            case Sensor.TYPE_GRAVITY:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 重力传感器:" + temp);
                break;
            case Sensor.TYPE_GYROSCOPE:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 陀螺仪传感器:" + temp);
                break;
            case Sensor.TYPE_LIGHT:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 环境光线传感器:" + temp);
                break;
            case Sensor.TYPE_LINEAR_ACCELERATION:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 线性加速器:" + temp);
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 电磁场传感器:" + temp);
                break;
            case Sensor.TYPE_ORIENTATION:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 方向传感器:" + temp);
                break;
            case Sensor.TYPE_PRESSURE:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 压力传感器:" + temp);
                break;
            case Sensor.TYPE_PROXIMITY:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 距离传感器:" + temp);
                break;
            case Sensor.TYPE_ROTATION_VECTOR:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 旋转向量:" + temp);
                break;
            case Sensor.TYPE_TEMPERATURE:
                tv_view.setText(tv_view.getText().toString() + s.getType()
                        + " 温度传感器:" + temp);
                break;
            default:
                tv_view.setText(tv_view.getText().toString() + s.getType() + " 未知传感器:"
                        + temp);
                break;
            }
        }
        
        // 在title上显示重力传感器的变化 
        sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        SensorEventListener lsn = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent e) {
                // TODO Auto-generated method stub
                x = e.values[SensorManager.DATA_X];
                y = e.values[SensorManager.DATA_Y];
                z = e.values[SensorManager.DATA_Z];
                setTitle("x=" + (int) x + "," + "y=" + (int) y + "," + "z="+ (int) z);
            }
            
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub
            }
        };
        // 注册listener,第三个参数是检测的精确度   
        sm.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
    }

}

 

效果图:

转载于:https://www.cnblogs.com/androidsj/archive/2013/04/08/3008351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值