光线传感器的使用实例:
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) {
}
};
}
对应的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.syberos.learnandroid.LightActivity">
<TextView
android:id="@+id/lightLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
加速度传感器的使用实例:
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) {
}
};
}
加速度传感器实例对应的布局文件代码如下(默认生成的,实例中并没有用到):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.syberos.learnandroid.AccelerometerActivity">
</android.support.constraint.ConstraintLayout>
利用加速传感器和方向传感器实现的简单的指南针的代码:
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) {
}
};
}
指南针实例的布局文件的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.syberos.learnandroid.CompassActivity">
<ImageView
android:id="@+id/compass_img"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/compass"
/>
<ImageView
android:id="@+id/arrow_img"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow" />
</RelativeLayout>
使用的图片如下:
这两个图片都是从网上下载的,版权属于原作者
调用这几个 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<Map<String, String>> functionInfo;
private static final String KEY_TITLE = "title";
private static final String KEY_CONTENT = "content";
static {
functionInfo = new ArrayList<>();
Map<String, String> tmpMap = new ArrayMap<>();
tmpMap.put(KEY_TITLE, "Light");
tmpMap.put(KEY_CONTENT, "Light Sensor Test");
functionInfo.add(tmpMap);
tmpMap = new ArrayMap<>();
tmpMap.put(KEY_TITLE, "Accelerometer");
tmpMap.put(KEY_CONTENT, "Accelerometer Sensor Test");
functionInfo.add(tmpMap);
tmpMap = new ArrayMap<>();
tmpMap.put(KEY_TITLE, "Compass");
tmpMap.put(KEY_CONTENT, "Compass Sensor Test");
functionInfo.add(tmpMap);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(new MyAdapter());
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyUp(keyCode, event);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return functionInfo.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup container) {
if (functionInfo.size() <= 0) {
return convertView;
}
ViewHolder holder;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, container,
false);
holder = new ViewHolder();
holder.tv_title = convertView.findViewById(R.id.title);
holder.tv_content = convertView.findViewById(R.id.content);
convertView.setTag(holder);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "current clicked position is " + position);
openActivity(position);
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_title.setText(functionInfo.get(position).get(KEY_TITLE));
holder.tv_content.setText(functionInfo.get(position).get(KEY_CONTENT));
return convertView;
}
}
private static class ViewHolder {
public TextView tv_title;
public TextView tv_content;
}
private void openActivity(int position) {
Intent intent = null;
switch (position) {
case 0:
intent = new Intent(this, LightActivity.class);
break;
case 1:
intent = new Intent(this, AccelerometerActivity.class);
break;
case 2:
intent = new Intent(this, CompassActivity.class);
break;
default:
break;
}
if (null != intent) {
startActivity(intent);
}
}
}
其对应的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
其中用于布局 ListView 的每一项显示的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
本文参考自 《Android 第一行代码》