通过获取系统服务得到SensorManager
注册一个监听器SensorEventListener,数据处理过程就在监听器里面
光线触感器
package com.itheima.sensor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private SensorManager sm;
private MyListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取系统服务
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
//光线传感器 参数指定获取什么类型的传感器,也可以指定获取所有传感器
Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
listener = new MyListener();
//注册一个监听器
//第三参数代表采样频率,频率越高,精度越高,越费电
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
}
private class MyListener implements SensorEventListener{
@Override
public void onSensorChanged(SensorEvent event) {
//不同类型的传感器,values的意义是不一样的 这里指的是光线的强弱
float light = event.values[0];
System.out.println("light:"+light);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
@Override
protected void onDestroy() {
//应用程序关掉之后传感器还会一直在工作,所以需要把监听器注销掉并且置为null
sm.unregisterListener(listener);
listener = null;
super.onDestroy();
}
}
方向传感器
<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"
android:background="#000000"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/zn" />
</RelativeLayout>
package com.itheima.sensor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
private SensorManager sm;
private MyListener listener;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
iv = (ImageView) findViewById(R.id.iv);
//方向传感器 虽然过时了,但是为了兼容低版本,还是用这个
Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
listener = new MyListener();
//SENSOR_DELAY_GAME改成游戏模式的采集频率
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
}
private class MyListener implements SensorEventListener{
float lastangle = 0;
@Override
public void onSensorChanged(SensorEvent event) {
// 0=North, 90=East, 180=South, 270=West
float angle = event.values[0];//手机与正北方向的夹角
System.out.println("angle:"+angle);
RotateAnimation ra = new RotateAnimation(-lastangle, angle,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
iv.startAnimation(ra);
lastangle = angle;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
@Override
protected void onDestroy() {
sm.unregisterListener(listener);
listener = null;
super.onDestroy();
}
}