欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

android传感器(加速度、方位、光线)示例源码

介绍 评论 失效链接反馈

from clipboardpackage com.example.admin.myapplication;import android.app.Activity;import android.content.Intent;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor mAccelerometer; private Sensor mOrientation; private Sensor mLight; private TextView tAccelerometer; private TextView tOrientation; private TextView tLight; private Button mcontinue; private Button mpause;int b; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mcontinue = (Button) findViewById(R.id.button1); mpause = (Button) findViewById(R.id.button2);mcontinue.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { b=1; }});mpause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {b=2; }}); tAccelerometer = (TextView) this.findViewById(R.id.tAccelerometer); tOrientation = (TextView) this.findViewById(R.id.tOrientation); tLight = (TextView) this.findViewById(R.id.tLight); // 获得传感器管理器 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); //获得加速度传感器 mAccelerometer = mSensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //获得方向度传感器 mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //获得光线传感器 mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); } protected void onResume() { super.onResume(); //对加速度传感器注册传感器监听器 mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); //对方向传感器注册传感器监听器 mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL); //对光线传感器注册传感器监听器 mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { super.onPause(); // 取消传感器监听器的注册 mSensorManager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { // 在此方法中,编写当某个传感器的精度发生变化时应执行的操作 } public void onSensorChanged(SensorEvent event) { // 在此方法中,编写当某个传感器的数值发生变化时应执行的操作 // 得到方向的值 if (b==1) { float x = event.values[1]; float y = event.values[2]; float z = event.values[3]; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { tOrientation.setText("方位: " x ", " y ", " z); } // 得到加速度的值 else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { tAccelerometer.setText("加速度: " x ", " y ", " z); } else if (event.sensor.getType() == Sensor.TYPE_LIGHT) { tLight.setText("光线: " event.values[0]); } } else if(b==2){ tOrientation.setText("方位: " ); tAccelerometer.setText("加速度: " ); tLight.setText("光线: " ); } }}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复