光线传感器的使用实例:
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class LightActivity extends Activity {
private SensorManager sensorManager;
private TextView lightLevel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light);
lightLevel = findViewById(R.id.lightLevel);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
if (sensorManager != null) {
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(sensorEventListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != sensorManager) {
sensorManager.unregisterListener(sensorEventListener);
}
}
private SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float value = sensorEvent.values[0];
lightLevel.setText(String.format("Current light level is %s lx", value));
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
对应的布局文件如下:
加速度传感器的使用实例:
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.Toast;
public class AccelerometerActivity extends Activity {
private static final int THRESHOLD_VALUE = 15;
private SensorManager sensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
if (sensorManager != null) {
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(sensorEventListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != sensorManager) {
sensorManager.unregisterListener(sensorEventListener);
}
}
private SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float x_value = Math.abs(sensorEvent.values[0]);
float y_value = Math.abs(sensorEvent.values[1]);
float z_value = Math.abs(sensorEvent.values[2]);
if (x_value > THRESHOLD_VALUE || y_value > THRESHOLD_VALUE
|| z_value > THRESHOLD_VALUE) {
Toast.makeText(AccelerometerActivity.this, "设备正在晃动...",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
加速度传感器实例对应的布局文件代码如下(默认生成的,实例中并没有用到):
利用加速传感器和方向传感器实现的简单的指南针的代码:
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
public class CompassActivity extends Activity {
private SensorManager sensorManager;
private ImageView iv_arrow;
private ImageView iv_compass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compass);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
if (sensorManager != null) {
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(sensorEventListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(sensorEventListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
iv_arrow = findViewById(R.id.arrow_img);
iv_compass = findViewById(R.id.compass_img);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != sensorManager) {
sensorManager.unregisterListener(sensorEventListener);
}
}
private SensorEventListener sensorEventListener = new SensorEventListener() {
float[] accelerometerValues = new float[3];
float[] magneticValues = new float[3];
float lastRotateDegree;
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accelerometerValues = sensorEvent.values.clone();
} else if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
magneticValues = sensorEvent.values.clone();
}
// 使用 clone 方法,避免 accelerometerValues 和 magneticValues 指向同一个引用
float[] R = new float[9];
float[] values = new float[3];
SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);
SensorManager.getOrientation(R, values);
float rotateDegree = -(float) Math.toDegrees(values[0]);
if (Math.abs(rotateDegree - lastRotateDegree) > 0) {
RotateAnimation animation = new RotateAnimation(lastRotateDegree, rotateDegree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setFillAfter(true);
iv_compass.startAnimation(animation);
lastRotateDegree = rotateDegree;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
指南针实例的布局文件的代码如下:
使用的图片如下:
这两个图片都是从网上下载的,版权属于原作者
调用这几个 Activity 的代码如下:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static List
其对应的布局文件如下:
其中用于布局 ListView 的每一项显示的布局文件如下:
本文参考自 《Android 第一行代码》