在本文中,我们将在Android中使用Java和XML构建一个运动传感器测试应用程序项目。该应用程序将使用设备的硬件来检测运动。检测运动所需的组件是加速度计和陀螺仪。加速度计是一种电子传感器,用于通过测量设备的加速度和运动来检测设备在空间中的位置。而陀螺仪会感应设备的角度运动。在此应用程序中,我们将在移动设备时观察屏幕颜色的变化。此应用程序中只有一个活动。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
第2步:首先进入编码部分,您必须做一些准备工作
在AndroidMainfest.xml中添加功能:我们需要添加此应用程序所需的硬件要求。我们需要的两个组件是陀螺仪和加速度计。将以下内容添加到您的AndroidMainfest.xml文件中。
为Accelerometer和Gyroscope创建两个Java类,并将该类命名为Accelerometer和Gyroscope 。
在themes.xml文件中将样式更改为NoActionBar:
步骤3:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
Java
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Accelerometer {
// create an interface with one method
public interface Listener {
// create method with all 3
// axis translation as argument
void onTranslation(float tx, float ty, float ts);
}
// create an instance
private Listener listener;
// method to set the instance
public void setListener(Listener l) {
listener = l;
}
private SensorManager sensorManager;
private Sensor sensor;
private SensorEventListener sensorEventListener;
// create constructor with
// context as argument
Accelerometer(Context context) {
// create instance of sensor manager
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
// create instance of sensor
// with type linear acceleration
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
// create the sensor listener
sensorEventListener = new SensorEventListener() {
// this method is called when the
// device's position changes
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// check if listener is
// different from null
if (listener != null) {
// pass the three floats in listener on translation of axis
listener.onTranslation(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
// create register method
// for sensor notifications
public void register() {
// call sensor manger's register listener
// and pass the required arguments
sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
// create method to unregister
// from sensor notifications
public void unregister() {
// call sensor manger's unregister listener
// and pass the required arguments
sensorManager.unregisterListener(sensorEventListener);
}
}
Java
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Gyroscope {
// create an interface with one method
public interface Listener {
// create method with all 3
// axis translation as argument
void onRotation(float tx, float ty, float ts);
}
// create an instance
private Listener listener;
// method to set the instance
public void setListener(Listener l) {
listener = l;
}
private SensorManager sensorManager;
private Sensor sensor;
private SensorEventListener sensorEventListener;
// create constructor with context as argument
Gyroscope(Context context) {
// create instance of sensor manager
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
// create instance of sensor with type gyroscope
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
// create the sensor listener
sensorEventListener = new SensorEventListener() {
// this method is called when
// the device's position changes
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// check if listener is different from null
if (listener != null) {
// pass the three floats in listener on rotation of axis
listener.onRotation(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
// create register method
// for sensor notifications
public void register() {
// call sensor manger's register listener and pass the required arguments
sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
// create method to unregister
// from sensor notifications
public void unregister() {
// call sensor manger's unregister listener
// and pass the required arguments
sensorManager.unregisterListener(sensorEventListener);
}
}
Java
import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// create variables of the two class
private Accelerometer accelerometer;
private Gyroscope gyroscope;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// instantiate them with this as context
accelerometer = new Accelerometer(this);
gyroscope = new Gyroscope(this);
// create a listener for accelerometer
accelerometer.setListener(new Accelerometer.Listener() {
//on translation method of accelerometer
@Override
public void onTranslation(float tx, float ty, float ts) {
// set the color red if the device moves in positive x axis
if (tx > 1.0f) {
getWindow().getDecorView().setBackgroundColor(Color.RED);
}
// set the color blue if the device moves in negative x axis
else if (tx < -1.0f) {
getWindow().getDecorView().setBackgroundColor(Color.BLUE);
}
}
});
// create a listener for gyroscope
gyroscope.setListener(new Gyroscope.Listener() {
// on rotation method of gyroscope
@Override
public void onRotation(float rx, float ry, float rz) {
// set the color green if the device rotates on positive z axis
if (rz > 1.0f) {
getWindow().getDecorView().setBackgroundColor(Color.GREEN);
}
// set the color yellow if the device rotates on positive z axis
else if (rz < -1.0f) {
getWindow().getDecorView().setBackgroundColor(Color.YELLOW);
}
}
});
}
// create on resume method
@Override
protected void onResume() {
super.onResume();
// this will send notification to
// both the sensors to register
accelerometer.register();
gyroscope.register();
}
// create on pause method
@Override
protected void onPause() {
super.onPause();
// this will send notification in
// both the sensors to unregister
accelerometer.unregister();
gyroscope.unregister();
}
}
步骤4:使用加速度计。 Java文件
在加速度计中。 Java文件,我们将创建函数以获取设备在X,Y和Z轴上的坐标。该函数会将所需的参数发送到传感器,并在返回值中获取坐标。我们将首先创建SensorManager , Sensor和SensorEventListener对象。 SensorManager和Sensor变量使我们可以访问设备的传感器,而每次设备位置发生变化和传感器被接合时,都会调用SensorEventListener。以下是加速度计的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Accelerometer {
// create an interface with one method
public interface Listener {
// create method with all 3
// axis translation as argument
void onTranslation(float tx, float ty, float ts);
}
// create an instance
private Listener listener;
// method to set the instance
public void setListener(Listener l) {
listener = l;
}
private SensorManager sensorManager;
private Sensor sensor;
private SensorEventListener sensorEventListener;
// create constructor with
// context as argument
Accelerometer(Context context) {
// create instance of sensor manager
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
// create instance of sensor
// with type linear acceleration
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
// create the sensor listener
sensorEventListener = new SensorEventListener() {
// this method is called when the
// device's position changes
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// check if listener is
// different from null
if (listener != null) {
// pass the three floats in listener on translation of axis
listener.onTranslation(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
// create register method
// for sensor notifications
public void register() {
// call sensor manger's register listener
// and pass the required arguments
sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
// create method to unregister
// from sensor notifications
public void unregister() {
// call sensor manger's unregister listener
// and pass the required arguments
sensorManager.unregisterListener(sensorEventListener);
}
}
步骤4:使用陀螺仪。 Java文件
在陀螺仪中。 Java文件,除了传感器将是陀螺仪类型而不是加速度计之外,其他所有内容都相同。同样,听众将旋转而不是平移。下面是陀螺仪的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Gyroscope {
// create an interface with one method
public interface Listener {
// create method with all 3
// axis translation as argument
void onRotation(float tx, float ty, float ts);
}
// create an instance
private Listener listener;
// method to set the instance
public void setListener(Listener l) {
listener = l;
}
private SensorManager sensorManager;
private Sensor sensor;
private SensorEventListener sensorEventListener;
// create constructor with context as argument
Gyroscope(Context context) {
// create instance of sensor manager
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
// create instance of sensor with type gyroscope
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
// create the sensor listener
sensorEventListener = new SensorEventListener() {
// this method is called when
// the device's position changes
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// check if listener is different from null
if (listener != null) {
// pass the three floats in listener on rotation of axis
listener.onRotation(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
// create register method
// for sensor notifications
public void register() {
// call sensor manger's register listener and pass the required arguments
sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
// create method to unregister
// from sensor notifications
public void unregister() {
// call sensor manger's unregister listener
// and pass the required arguments
sensorManager.unregisterListener(sensorEventListener);
}
}
步骤5:使用MainActivity。 Java文件
在MainActivity中。 Java文件,我们将创建两个类的侦听器函数。我们将根据设备的旋转或移动设置屏幕的背景色。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// create variables of the two class
private Accelerometer accelerometer;
private Gyroscope gyroscope;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// instantiate them with this as context
accelerometer = new Accelerometer(this);
gyroscope = new Gyroscope(this);
// create a listener for accelerometer
accelerometer.setListener(new Accelerometer.Listener() {
//on translation method of accelerometer
@Override
public void onTranslation(float tx, float ty, float ts) {
// set the color red if the device moves in positive x axis
if (tx > 1.0f) {
getWindow().getDecorView().setBackgroundColor(Color.RED);
}
// set the color blue if the device moves in negative x axis
else if (tx < -1.0f) {
getWindow().getDecorView().setBackgroundColor(Color.BLUE);
}
}
});
// create a listener for gyroscope
gyroscope.setListener(new Gyroscope.Listener() {
// on rotation method of gyroscope
@Override
public void onRotation(float rx, float ry, float rz) {
// set the color green if the device rotates on positive z axis
if (rz > 1.0f) {
getWindow().getDecorView().setBackgroundColor(Color.GREEN);
}
// set the color yellow if the device rotates on positive z axis
else if (rz < -1.0f) {
getWindow().getDecorView().setBackgroundColor(Color.YELLOW);
}
}
});
}
// create on resume method
@Override
protected void onResume() {
super.onResume();
// this will send notification to
// both the sensors to register
accelerometer.register();
gyroscope.register();
}
// create on pause method
@Override
protected void onPause() {
super.onPause();
// this will send notification in
// both the sensors to unregister
accelerometer.unregister();
gyroscope.unregister();
}
}