Android Sensor Shake (WeChat)

Source: Internet
Author: User

<uses-permission android:name= "Android.permission.VIBRATE"/>
Package Com.buzz.shake;import Android.app.activity;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import Android.hardware.SensorManager; Import Android.os.bundle;import android.os.handler;import Android.os.message;import Android.os.vibrator;import Android.util.log;import android.widget.toast;/** * "Shake" * @author Nickchung */public class Sensoractivity extends Activi ty {private Sensormanager sensormanager;private vibrator vibrator;private static final String TAG = "Sensoractivity";p Riv ATE static final int sensor_shake = 10;/** Called when the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Sensormanager = (Sensormanager) getsystemservice (sensor_service); vibrator = (Vibrator) Getsystemservice (Vibrator_service);} @Overrideprotected void Onresume () {super.onresume (); if (Sensormanager! = null) {//Register listener Sensormanager.registerliStener (Sensoreventlistener, Sensormanager.getdefaultsensor (sensor.type_accelerometer), SensorManager.SENSOR_ Delay_normal);///The first parameter is listener, the second parameter is the resulting sensor type, the third parameter value gets the frequency of the Sensor information}} @Overrideprotected void OnPause () {super.onpause () ; if (Sensormanager! = null) {//Cancels listener Sensormanager.unregisterlistener (Sensoreventlistener);}} /** * Gravity Sensing monitor */private sensoreventlistener Sensoreventlistener = new Sensoreventlistener () {@Overridepublic void onsensor  Changed (Sensorevent event) {//The sensor information is changed when the method is executed float[] values = event.values;float x = values[0];//x-axis direction of gravity, right is positive float y = VALUES[1]; The gravitational acceleration of the y-axis, forward is positive float z = values[2]; The gravitational acceleration of the z-axis, upward is positive log.i (TAG, "x-axis of gravity" + x + ", y-axis direction of the gravitational acceleration" + y + "; z-axis direction of the acceleration of gravity" + Z);//generally in these three directions the acceleration of gravity reached 40 to shake the phone state. int mediumvalue = 19;if (Math.Abs (x) > Mediumvalue | | Math.Abs (y) > Mediumvalue | | Math.Abs (z) > Mediumvalue) {vibrator.vibrate (200); Message msg = new Message (); msg.what = Sensor_shake;handler.sendmessage (msg);}} @Overridepublic void onaccuracychanged (Sensor sensor, int accuracy) {}};/** * action execution */handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {sup Er.handlemessage (msg); switch (msg.what) {case SENSOR_SHAKE:Toast.makeText (testsensoractivity.this), "Shake is detected, perform operation!" ", Toast.length_short). Show (); LOG.I (TAG, "shake detected, perform operation!") "); break;}};}

Ref: Shake-shake function, Android gravity sensor development

Android Sensor Shake (WeChat)

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.