Android Interview--Shake It

Source: Internet
Author: User

The realization of "shake a Shake" function, in fact, is very simple, is to detect the mobile phone's gravity sensor, the specific implementation code as follows:

First, add the Operation permission in the Androidmanifest.xml

<uses-permission android:name= "Android.permission.VIBRATE"/>

Second, the implementation of the Code

Package Com.xs.test;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;/** * Android Shaking phone monitoring--"shake" * * @author Tan Hongyu * */public class Testsensoractivity extends Activity {private Sensormanager sensormanager;private Vibrator vibrator;private static final String TAG = "testsensoractivity";p rivate STA Tic 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 medumvalue = 19;//Samsung i9250 No more than 20, no way, only set 19 if (Math.Abs (x) > Medumvalue | | Math.Abs (y) > Medumvalue | | Math.Abs (z) > Medumvalue) {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 () {@Override public void Handlemessage (Message msg) {super.handlemessage (msg), switch (msg.what) {case SENSOR_SHAKE:Toast.makeText ( Testsensoractivity.this, "The shaking is detected and the operation is performed!" ", Toast.length_short). Show (); LOG.I (TAG, "shake detected, perform operation!") "); break;}};}

Android Interview--Shake It

Related Article

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.