标签:android blog http io ar os 使用 sp java
public interface SensorEventListener { /** * Called when sensor values have changed. * <p>See {@link android.hardware.SensorManager SensorManager} * for details on possible sensor types. * <p>See also {@link android.hardware.SensorEvent SensorEvent}. * * <p><b>NOTE:</b> The application doesn't own the * {@link android.hardware.SensorEvent event} * object passed as a parameter and therefore cannot hold on to it. * The object may be part of an internal pool and may be reused by * the framework. * * @param event the {@link android.hardware.SensorEvent SensorEvent}. */ public void onSensorChanged(SensorEvent event); /** * Called when the accuracy of a sensor has changed. * <p>See {@link android.hardware.SensorManager SensorManager} * for details. * * @param accuracy The new accuracy of this sensor */ public void onAccuracyChanged(Sensor sensor, int accuracy); }
import android.app.Activity; import android.app.Service; import android.os.Vibrator; public class VibratorHelper { public static void Vibrate(final Activity activity, long milliseconds) { Vibrator vibrator = (Vibrator) activity .getSystemService(Service.VIBRATOR_SERVICE); vibrator.vibrate(milliseconds); } public static void Vibrate(final Activity activity, long[] pattern, boolean isRepeat) { Vibrator vibrator = (Vibrator) activity .getSystemService(Service.VIBRATOR_SERVICE); vibrator.vibrate(pattern, isRepeat ? 1 : -1); } }同时,还需要在AndroidManifest.xml里增加震动权限:
<uses-permission android:name="android.permission.VIBRATE"/>解释一下Vibrate方法的参数:
import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class MainActivity extends Activity { private SensorManager sensorManager; private SensorEventListener shakeListener; private AlertDialog.Builder dialogBuilder; private boolean isRefresh = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); shakeListener = new ShakeSensorListener(); dialogBuilder = new AlertDialog.Builder(this); dialogBuilder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { isRefresh = false; dialog.cancel(); } }).setMessage("摇到了一个漂亮妹子!").create(); } @Override protected void onResume() { sensorManager.registerListener(shakeListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST); super.onResume(); } @Override protected void onPause() { // acitivity后台时取消监听 sensorManager.unregisterListener(shakeListener); super.onPause(); } private class ShakeSensorListener implements SensorEventListener { private static final int ACCELERATE_VALUE = 20; @Override public void onSensorChanged(SensorEvent event) { // Log.e("zhengyi.wzy", "type is :" + event.sensor.getType()); // 判断是否处于刷新状态(例如微信中的查找附近人) if (isRefresh) { return; } float[] values = event.values; /** * 一般在这三个方向的重力加速度达到20就达到了摇晃手机的状态 x : x轴方向的重力加速度,向右为正 y : * y轴方向的重力加速度,向前为正 z : z轴方向的重力加速度,向上为正 */ float x = Math.abs(values[0]); float y = Math.abs(values[1]); float z = Math.abs(values[2]); Log.e("zhengyi.wzy", "x is :" + x + " y is :" + y + " z is :" + z); if (x >= ACCELERATE_VALUE || y >= ACCELERATE_VALUE || z >= ACCELERATE_VALUE) { Toast.makeText( MainActivity.this, "accelerate speed :" + (x >= ACCELERATE_VALUE ? x : y >= ACCELERATE_VALUE ? y : z), Toast.LENGTH_SHORT).show(); VibratorHelper.Vibrate(MainActivity.this, 300); isRefresh = true; dialogBuilder.show(); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } } }
标签:android blog http io ar os 使用 sp java
原文地址:http://blog.csdn.net/wzy_1988/article/details/41211431