Monitor phone shake (shake) Sensoreventlistener

Source: Internet
Author: User

  1. Import Android.content.Context;
  2. Import Android.hardware.Sensor;
  3. Import android.hardware.SensorEvent;
  4. Import Android.hardware.SensorEventListener;
  5. Import Android.hardware.SensorManager;
  6. /**
  7. *
  8. * A monitor that detects phone shake
  9. * @author Fuzhengchao
  10. *
  11. */
  12. Public class Shakelistener implements Sensoreventlistener {
  13. //Speed threshold, which acts when the shaking speed reaches this value
  14. private static final int speed_shreshold = 4000;
  15. //Two time interval of detection
  16. private static final int uptate_interval_time = 70;
  17. //Sensor manager
  18. private Sensormanager Sensormanager;
  19. //Sensor
  20. private sensor sensor;
  21. //Gravity sensor listener
  22. private Onshakelistener Onshakelistener;
  23. //Context
  24. private context context;
  25. //Cell phone position when the gravity sensor coordinates
  26. private float lastx;
  27. private float lasty;
  28. private float Lastz;
  29. //Last detection time
  30. private long lastupdatetime;
  31. //Builder
  32. Public Shakelistener (Context c) {
  33. //Get Listener Object
  34. context = C;
  35. Start ();
  36. }
  37. //Start
  38. public void Start () {
  39. //Get the Sensor manager
  40. Sensormanager = (Sensormanager) context.getsystemservice (Context.sensor_service);
  41. if (sensormanager! = null) {
  42. //Get gravity sensor
  43. Sensor = Sensormanager.getdefaultsensor (Sensor.type_accelerometer);
  44. }
  45. //Registration
  46. if (sensor! = null) {
  47. Sensormanager.registerlistener (this, sensor, sensormanager.sensor_delay_game);
  48. }
  49. }
  50. //Stop detection
  51. public void Stop () {
  52. Sensormanager.unregisterlistener (this);
  53. }
  54. //Shake Monitoring interface
  55. Public interface Onshakelistener {
  56. public void Onshake ();
  57. }
  58. //Set Gravity Sensor listener
  59. public void Setonshakelistener (Onshakelistener listener) {
  60. Onshakelistener = listener;
  61. }
  62. //gravity sensor sensing for change data
  63. public void Onsensorchanged (Sensorevent event) {
  64. //Current detection time
  65. Long currentupdatetime = System.currenttimemillis ();
  66. //Two time interval of detection
  67. long timeinterval = Currentupdatetime-lastupdatetime;
  68. //Determine if the detection interval is reached
  69. if (TimeInterval < uptate_interval_time)
  70. return;
  71. //The time now becomes last time
  72. LastUpdateTime = Currentupdatetime;
  73. //Get x, y, Z coordinates
  74. float x = event.values[0];
  75. float y = event.values[1];
  76. float z = event.values[2];
  77. //Get change values for x, Y, z
  78. float deltax = x-lastx;
  79. float deltay = y-lasty;
  80. float Deltaz = Z-lastz;
  81. //Turn the current coordinates into the last coordinates
  82. LASTX = x;
  83. Lasty = y;
  84. Lastz = Z;
  85. Double speed = math.sqrt (deltax*deltax + deltay*deltay + Deltaz*deltaz)/timeinterval * 10000;
  86. //To reach the speed threshold, send a hint
  87. if (speed >= speed_shreshold)
  88. Onshakelistener.onshake ();
  89. }
  90. public void onaccuracychanged (sensor sensor, int accuracy) {
  91. }
  92. }



Usage:

Java Code
  1. Shakelistener Shakelistener = new Shakelistener (this); Create an object
  2. Shakelistener.setonshakelistener (new Onshakelistener () {//Call Setonshakelistener method for listening
  3. Public void Onshake () {
  4. //The handling of the mobile phone after shaking (such as changing songs, changing pictures, shaking ...) )
  5. //onvibrator ();
  6. }
  7. });



Vibration

Java Code
  1. Private void Onvibrator () {
  2. Vibrator Vibrator = (Vibrator) context.getsystemservice (Context.vibrator_service);
  3. if (vibrator = = null) {
  4. Vibrator Localvibrator = (vibrator) Context.getapplicationcontext ()
  5. . Getsystemservice ("Vibrator");
  6. Vibrator = Localvibrator;
  7. }
  8. Vibrator.vibrate (100L);
  9. }



Shake a thin Code implementation
Http://blog.sina.com.cn/s/blog_66cfbaa5010120w8.html

Android similar to Qihoo 360 mobile phone in the guardian of the shaking effect achieved
http://blog.csdn.net/nono_love_lilith/article/details/7554341

Monitor phone shake (shake) Sensoreventlistener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.