Android 摇一摇功能的实现

package com.tao.tools.shack;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class ShackListener implements SensorEventListener {

	public static final int SPEED_SHRESHOLD = 3000;// 摇晃起作用的临界值
	public static final int UPTATE_INTERVAL_TIME = 100;// 两次检测的时间间隔

	private SensorManager sensorManager;// 传感器管理器
	private Sensor sensor;// 传感器
	private OnShackListener onShackListener;// 重力感应监听器
	private Context context;
	// 手机上某一点向3个坐标的坐标
	private float lastX;
	private float lastY;
	private float lastZ;
	private long lastUpdateTime;// 上次更新时间

	public ShackListener(Context context) {
		this.context = context;
		start();
	}

	/**
	 * 获取传感器
	 */
	public void start() {
		sensorManager = (SensorManager) context
				.getSystemService(Context.SENSOR_SERVICE);
		// 获取重力传感器
		if (sensorManager != null) {
			sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		}
		// 注册重力传感器
		if (sensor != null) {
			sensorManager.registerListener(this, sensor,
					SensorManager.SENSOR_DELAY_GAME);
		}
	}

	/**
	 * 停止检测,释放传感器管理器资源
	 */
	public void stop() {
		sensorManager.unregisterListener(this);
	}

	public void setOnShackListener(OnShackListener shackListener) {
		this.onShackListener = shackListener;
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		long currentUpdateTime = System.currentTimeMillis();// 当前时间
		long timeInterval = currentUpdateTime - lastUpdateTime;// 两次检测时间间隔
		if (timeInterval < this.UPTATE_INTERVAL_TIME) {
			return;
		}
		lastUpdateTime = currentUpdateTime;
		// 获得3个方向的坐标
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];
		// 3个方向的变化量
		float deltaX = x - lastX;
		float deltaY = y - lastY;
		float deltaZ = z - lastZ;

		lastX = x;
		lastY = y;
		lastZ = z;

		double speed = Math.sqrt(lastX * lastX + lastY * lastY + lastZ * lastZ)
				/ timeInterval * 10000;
		
		if (speed >= this.SPEED_SHRESHOLD) {
			onShackListener.onShack();
		}

	}

	public interface OnShackListener {
		public void onShack();
	}

}


package com.tao.tools.shack;

import com.tao.tools.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class ShackActivity extends Activity {
	private ShackListener mShackListener = null;
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示程序的标题栏 
		setContentView(R.layout.activity_shackactivity);
		mShackListener = new ShackListener(this);
		setListener();
		textView = (TextView)findViewById(R.id.text);
	}
	
	private void setListener(){
		mShackListener.setOnShackListener(new ShackListener.OnShackListener() {
			
			@Override
			public void onShack() {
//				Toast.makeText(ShackActivity.this, "You shake", Toast.LENGTH_LONG).show(); 
				textView.setText("");
				textView.setText("vvvvvvvvvvvvvv");
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mShackListener.stop();
	}

	
	
}



你可能感兴趣的:(android)