Android shake-shake function implementation

Source: Internet
Author: User

Android uses sensors to achieve a similar shake-and-shake function

1. Fundamentals

The basic principle of shaking the function is to use the mobile accelerometer, when the acceleration reaches a certain value, trigger an event, such as UI changes, phone shake, play sound, get data and other events.

2. Sensor principle and use

The principle and use of sensors can be found in the previous blog, the use of sensor sensors in Android

3. Sound Events

Gets the object Mediaplay class Mediaplayer=mediaplayer.create (Getapplicationcontext (), r.raw.music);

Event Trigger: Mediaplayer.start ();

4. Vibration Events

Get Vibrator Object, vibrator= (vibrator) Getsystemservice (Vibrator_service);

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

Event Trigger: vibrator.vibrate (200);

5. Code implementation

5.1 Initializing the Management object Sensormanager,vibrator,mediaplayer

1 sensormanager=(Sensormanager) Getsystemservice (sensor_service); 2 vibrator=(vibrator) Getsystemservice (vibrator_service); 3 mediaplayer=mediaplayer.create (Getapplicationcontext (), r.raw.music);

R.raw.music is the sound material, raw directory.

5.2 Creating a sensor event listener anonymous class

1 PrivateSensoreventlistener sensoreventlistener=NewSensoreventlistener () {2         3 @Override4          Public voidonsensorchanged (Sensorevent event) {5             6             7             float[] values=event.values;8             //generally in these three directions the acceleration of gravity reached 40 to the state of shaking the phone. 9              floatx = Values[0];//gravitational acceleration in the x-axis direction, rightTen              floaty = values[1];//the gravitational acceleration of the y-axis, forward positive One              floatz = values[2];//the gravitational acceleration of the z-axis, upward is positive A             intMedumvalue = 10; -             if(Math.Abs (x) > Medumvalue | | Math.Abs (y) > Medumvalue | | Math.Abs (z) >medumvalue) -             {   theLOG.I ("tag", "x=" +values[0]+ ", y=" +values[1]+ ", z=" +values[2]); -Vibrator.vibrate (200);  -Message msg=NewMessage (); -msg.what=Sensor_shake; + handler.sendmessage (msg); - Mediaplayer.start (); +             } A              at         } -          - @Override -          Public voidOnaccuracychanged (sensor sensor,intaccuracy) { -             //TODO auto-generated Method Stub -              in         } -};

5.3Handler processing messages

1 PrivateHandler handler=NewHandler () {2 @Override3          Public voidhandlemessage (Message msg) {4             Super. Handlemessage (msg);5             Switch(msg.what) {6              CaseSensor_shake:7Toast.maketext (Getapplicationcontext (), "mobile phone shaking", Toast.length_long). Show ();8                 9                  Break;Ten  One             default: A                  Break; -             } -         } the};

5.4 Registering and shutting down monitoring

1 @Override2     protected voidOnresume () {3         Super. Onresume ();4         if(sensormanager!=NULL){5 Sensormanager.registerlistener (Sensoreventlistener, Sensormanager.getdefaultsensor (Sensor.TYPE_ACCELEROMET ER), sensormanager.sensor_delay_normal);6         7         }8     }9 @OverrideTen     protected voidOnPause () { One         Super. OnPause (); A         if(sensormanager!=NULL){ - Sensormanager.unregisterlistener (sensoreventlistener); -         } the}

6. Sample code Download

Http://pan.baidu.com/s/1bns5bUB

7. Run the interface

Android shake-shake function implementation

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.