MainActivity.java
ShakeListener.java
manifest.xml
package com.sensor;
import com.sensor.ShakeListener.OnShakeListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private ShakeListener shakeListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shakeListener=new ShakeListener(this);
shakeListener.setOnShakeListener(new Shake());
}
class Shake implements OnShakeListener{
@Override
public void onShake() {
// TODO Auto-generated method stub
setTitle("正在摇动了手机");
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
shakeListener.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
shakeListener.stop();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
shakeListener.stop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
ShakeListener.java
package com.sensor;
import android.app.Service;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Vibrator;
public class ShakeListener implements SensorEventListener {
private static final int SPEED_SHRESHOLD=3000;
private static final int UPDATE_INTERVAL_TIME=78;
private SensorManager sensorManager;
private Sensor sensor;
private Vibrator vibrator;
private OnShakeListener onShakeListener;
private Context context;
private float lastx;
private float lasty;
private float lastz;
private long lastUpdateTime;
public ShakeListener(Context context) {
super();
this.context = context;
start();
}
public void start() {
// TODO Auto-generated method stub
sensorManager=(SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
vibrator=(Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
if(sensorManager!=null){
sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
if(sensor!=null){
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
}
}
public void stop() {
// TODO Auto-generated method stub
sensorManager.unregisterListener(this);
}
public void setOnShakeListener(OnShakeListener listener){
onShakeListener=listener;
}
public interface OnShakeListener{
public void onShake();
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
long currentUpdateTime=System.currentTimeMillis();
long timeInterval=currentUpdateTime-lastUpdateTime;
if(timeInterval<UPDATE_INTERVAL_TIME){
return;
}
lastUpdateTime=currentUpdateTime;
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
float deltaX=x-lastx;
float deltaY=y-lasty;
float deltaZ=z-lastz;
lastx=x;
lasty=y;
lastz=z;
double speed=Math.sqrt(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ)/timeInterval*10000;
if(speed>=SPEED_SHRESHOLD){
onShakeListener.onShake();
vibrator.vibrate(1000);
}
}
private boolean coordinate(Float x,Float y,Float z){
if((x>10&&y>10)||(x>10&&z>10)||(y>10&&z>10)){
return true;
}else{
return false;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
vibrator.vibrate(0);
}
}
manifest.xml
<uses-permission android:name="android.permission.VIBRATE"/>