基于Android 的手机传感器检测

效果展示:

基于Android 的手机传感器检测_第1张图片基于Android 的手机传感器检测_第2张图片

可借用代码部分:

首先在布局文件写一个textView来显示他跳的数值

 在主文件中首先先定义一下

  private TextView mTextViewGravity;
 private SensorEventListener mEventListenerGravity;

 绑定按钮

    mTextViewGravity= (TextView) findViewById(R.id.text_gravity);

填写检测传感器的代码:

 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        initListeners();
 private  void initListeners(){
        final DecimalFormat df=new DecimalFormat("0.0");
        mEventListenerAccelerometer= new SensorEventListener() {
            //加速度传感器
            @Override
            public void onSensorChanged(SensorEvent event) {
                float[] values =event.values ;
                mTextViewAccelerometer . setText("Accelerometer:"+ df . format(values[0])
                        + ","+ df.format(values[1]) +","+ df.format(values[2]));
            }
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
            };

 填写可以让他关闭的代码

 protected void onResume() {
                    super.onResume();
                    mSensorManager.registerListener(mEventListenerAccelerometer,
                            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                            SensorManager.SENSOR_DELAY_NORMAL);
                    mSensorManager.registerListener(mEvenListenerLinearAcceleration,
                            mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
                            SensorManager.SENSOR_DELAY_NORMAL);
                    mSensorManager.registerListener(mEventListenerGravity,
                            mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
                            SensorManager.SENSOR_DELAY_NORMAL);
                }
                    @Override
                    protected void onStop() {
                        mSensorManager.unregisterListener(mEventListenerAccelerometer);
                        mSensorManager.unregisterListener(mEvenListenerLinearAcceleration);
                        mSensorManager.unregisterListener(mEventListenerGravity);
                        super.onStop();


                    }
}

MainActivity.java代码:

package com.huangfushi.hfs192025214_huoquchuanganqishuzhi;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends Activity {
   private SensorManager mSensorManager;
    private  TextView mTextViewAccelerometer;
    private TextView mTextViewLinearAcceleration;

    private SensorEventListener mEventListenerAccelerometer;
    private SensorEventListener mEvenListenerLinearAcceleration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextViewAccelerometer = (TextView) findViewById(R.id.text_accelerometer);
        mTextViewLinearAcceleration = (TextView) findViewById(R.id.text_linear_acceleration);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        initListeners();
    }
    private  void initListeners(){
        final DecimalFormat df=new DecimalFormat("0.0");
        mEventListenerAccelerometer= new SensorEventListener() {
            //加速度传感器
            @Override
            public void onSensorChanged(SensorEvent event) {
                float[] values =event.values ;
                mTextViewAccelerometer . setText("Accelerometer:"+ df . format(values[0])
                        + ","+ df.format(values[1]) +","+ df.format(values[2]));
            }
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
            };

            mEvenListenerLinearAcceleration = new SensorEventListener() {
                @Override
                public void onSensorChanged(SensorEvent event) {
                    float[] values = event.values;
                    mTextViewLinearAcceleration.setText("Linear Acceleration:" + df.format(values[0])
                            + "," + df.format(values[1]) + "," + df.format(values[2]));
                }
                    @Override

                    public void onAccuracyChanged(Sensor sensor,int accuracy){
                    }


                    };
                }

                protected void onResume() {
                    super.onResume();
                    mSensorManager.registerListener(mEventListenerAccelerometer,
                            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                            SensorManager.SENSOR_DELAY_NORMAL);
                    mSensorManager.registerListener(mEvenListenerLinearAcceleration,
                            mSensorManager
                                    .getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
                            SensorManager.SENSOR_DELAY_NORMAL);
                }
                    @Override
                    protected void onStop() {
                        mSensorManager.unregisterListener(mEventListenerAccelerometer);
                        mSensorManager.unregisterListener(mEvenListenerLinearAcceleration);
                        super .onStop();


                    }
}

 

Activity_main代码如下




    
    


你可能感兴趣的:(Android,studio,android,java,开发语言)