阅读android有关sensor的源码总结

       虽然这篇文章写得很差,因为赶时间,所以就匆匆忙忙地写出来自己作一个笔记。但是我想对大家应该有一点帮助。

1、有关sensorJava应用程序的编程(以注册多个传感器为例,这程序是我临时弄出来的,可能有错)

package com.sensors.acc;

 

import android.app.Activity;

import android.os.Bundle;

 

import android.util.Log;

import android.widget.TextView;

import android.hardware.SensorManager;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

 

public class acc extends Activity {

    float x, y, z;

    SensorManager sensormanager = null;

    Sensor accSensor = null;

    Sensor lightSensor = null;

    Sensor proximitySensor = null;

    TextView accTextView = null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    sensormanager = (SensorManager)getSystemService(SENSOR_SERVICE);

    accSensor = sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    lightSensor = sensormanager.getDefaultSensor(Sensor.TYPE_LIGHT);

    proximitySensor = sensormanager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

    accTextView = (TextView)findViewById(R.id.textview_name);

    }

   

    SensorEventListener lsn = new SensorEventListener() {   

        public void onSensorChanged(SensorEvent e) {

        if (e.sensor == accSensor) {

             Log.d("sensor", "found acc sensor");

                x = e.values[SensorManager.DATA_X];  

                y = e.values[SensorManager.DATA_Y];  

                z = e.values[SensorManager.DATA_Z];

                accTextView.setText("x = " + x + ", /ny = " + y + ", /nz = " + z);

        }

        else if (e.sensor == lightSensor) {

             Log.d("sensor", "found light sensor");

                accTextView.setText("data is " + e.values[0]);

        }

        else if (e.sensor == proximitySensor) {

             Log.d("sensor", "found proximity sensor");

                accTextView.setText("distance is " + e.values[0]);

        }

  //          Log.d("sensor", "found acc sensor");

 

 //           Log.d("sensor", "x = " + x + ", y = " + y + ", z = " + z);

 //           accTextView.setText("x = " + x + ", /<

  • 0
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 29
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值