MainActivity
public class MainActivity extends Activity implements SensorEventListener { TextView tv = null; SensorManager sensorManager = null; Vibrator vibrator = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { //当传感器精度改变时回调该方法,Do nothing. } @Override public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); //values[0]:X轴,values[1]:Y轴,values[2]:Z轴 float[] values = event.values; if (sensorType == Sensor.TYPE_ACCELEROMETER) { if ((Math.abs(values[0]) > 17 || Math.abs(values[1]) > 17 || Math .abs(values[2]) > 17)) { // Log.d("sensor x ", "============ values[0] = " + values[0]); // Log.d("sensor y ", "============ values[1] = " + values[1]); // Log.d("sensor z ", "============ values[2] = " + values[2]); tv.setText("摇一摇成功!!!"); //摇动手机后,再伴随震动提示~~ vibrator.vibrate(500); Intent intent = new Intent(this,Main2Activity.class); startActivity(intent); } } } }
activity_main 布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="页面1"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="页面 1 " android:textSize="40sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
Main2Activity
public class Main2Activity extends Activity implements SensorEventListener { //速度阈值,当摇晃速度达到这值后产生作用 private static final int SPEED_SHRESHOLD = 200; private int TIME = 70; SensorManager sensorManager = null; Vibrator vibrator = null; //手机上一个位置时重力感应坐标 private float lastX; private float lastY; private float lastZ; //上次检测时间 private long lastUpdateTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Toast.makeText(this, "页面2", Toast.LENGTH_SHORT).show(); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); lastUpdateTime = System.currentTimeMillis(); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onSensorChanged(SensorEvent event) { //现在检测时间 long currentUpdateTime = System.currentTimeMillis(); // Log.d("=====currentUpdateTime", "currentUpdateTime: --> " + currentUpdateTime); // Log.d("======lastUpdateTime", "lastUpdateTime: --> " + lastUpdateTime); //两次检测的时间间隔 long timeInterval = currentUpdateTime - lastUpdateTime; Log.d("==========timeInterval", "timeInterval:" + timeInterval); //判断是否达到了检测时间间隔 if(timeInterval < TIME&&lastUpdateTime!=0) return; //现在的时间变成last时间 lastUpdateTime = currentUpdateTime; //获得x,y,z坐标 float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; //获得x,y,z的变化值 float deltaX = x - lastX; float deltaY = y - lastY; float deltaZ = z - lastZ; //将现在的坐标变成last坐标 lastX = x; lastY = y; lastZ = z; double speed = Math.sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ)/timeInterval * 10000; //达到速度阀值,发出提示 if(speed >= SPEED_SHRESHOLD) Log.d("=========speed", "正在晃动"); else { Toast.makeText(this, "停止摇动", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this,MainActivity.class); startActivity(intent); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // Log.d("==========accuracy", accuracy + ""); // if (accuracy>0&&accuracy<3){ // Toast.makeText(this, "停止摇动", Toast.LENGTH_SHORT).show(); // Intent intent = new Intent(this,MainActivity.class); // startActivity(intent); // } } }
activity_main2 布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.Main2Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="页面2"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="页面 2 " android:textSize="40sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>