import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private static final String TAG = "MainActivity";
private SensorManager mManager;
private TextView mTv1;
private TextView mTv2;
private TextView mTv3;
private TextView mTv4;
private TextView mTv5;
private TextView mTv6;
private TextView mTv7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mManager = (SensorManager) getSystemService(SENSOR_SERVICE);
//获取传感器列表
List<Sensor> sensorList = mManager.getSensorList(Sensor.TYPE_ALL);
for (int i = 0; i < sensorList.size(); i++) {
Sensor sensor = sensorList.get(i);
Log.d(TAG, "type: " + sensor.getType() + ",name:" + sensor.getName());
}
}
@Override
protected void onResume() {
super.onResume();
/*
* 最常用的一个方法 注册事件
* 参数1 :SensorEventListener监听器
* 参数2 :Sensor 一个服务可能有多个Sensor实现,此处调用getDefaultSensor获取默认的Sensor
* 参数3 :模式 可选数据变化的刷新频率,多少微秒取一次。
* */
mManager.registerListener(this, mManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
//方向传感器
mManager.registerListener(this, mManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
//光线传感器
mManager.registerListener(this, mManager.getDefaultSensor(Sensor.TYPE_LIGHT),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mManager.unregisterListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
/**
* 传感器数据改变的回调
*
* @param event
*/
@Override
public void onSensorChanged(SensorEvent event) {
//event.values,传感器的数据
//event.timestamp 时间戳,时间
//event.sensor 传感器
//event.accuracy //精度
int type = event.sensor.getType();
if (type == Sensor.TYPE_ACCELEROMETER) {
//加速度传感器,将加速度分解成三个方向
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
Log.d(TAG, "x: " + x + ",y:" + y + ",z:" + z);
mTv1.setText("x轴:" + x);
mTv2.setText("y轴:" + y);
mTv3.setText("z轴:" + z);
//摇一摇
if (Math.abs(x) > 20 || Math.abs(y) > 20 || Math.abs(z) > 20) {
//一旦某个方向的加速度超过20,代表摇一摇了
shake();
}
} else if (type == Sensor.TYPE_ORIENTATION) {
//绕z轴转过的角度
float z = event.values[0];
float x = event.values[1];
float y = event.values[2];
//0度或者360度的时候指向北方
mTv4.setText("绕Z轴:" + z);
mTv5.setText("绕X轴:" + x);
mTv6.setText("绕Y轴:" + y);
} else if (type == Sensor.TYPE_LIGHT) {
//光照强度
float value = event.values[0];
mTv7.setText("光照强度:"+value);
}
}
private void shake() {
//音效+震动
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.weichat_audio);
mediaPlayer.start();
//震动
Vibrator vibrate = (Vibrator) getSystemService(VIBRATOR_SERVICE);
//方式1,震动1s
//vibrate.vibrate(1000);
//方式2
//这里使用的是一个长整型数组,数组的a[0]表示静止的时间,
// a[1]代表的是震动的时间,然后数组的a[2]表示静止的时间,
// a[3]代表的是震动的时间……依次类推下去,然后这里的代码有一点小小的改变:
long[] patter = {1000, 1000, 2000, 500};
vibrate.vibrate(patter, -1);
}
/**
* 传感器精度发生改变的回调
*
* @param sensor
* @param accuracy
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private void initView() {
mTv1 = (TextView) findViewById(R.id.tv1);
mTv2 = (TextView) findViewById(R.id.tv2);
mTv3 = (TextView) findViewById(R.id.tv3);
mTv4 = (TextView) findViewById(R.id.tv4);
mTv5 = (TextView) findViewById(R.id.tv5);
mTv6 = (TextView) findViewById(R.id.tv6);
mTv7 = (TextView) findViewById(R.id.tv7);
}
}
网友评论