Android Combat Simple Tutorial-the 31st gun (based on the acceleration sensor's shake-and-shake function example)

Source: Internet
Author: User

Let's take a look at the simulation of the shaking function, we are based on the mobile phone accelerometer to achieve this function, the implementation of the method is relatively simple.
Mainactivity.java:

Mainactivity.java: Packagecom.location.activity;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.hardware.Sensor;ImportAndroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;ImportAndroid.os.Bundle;ImportAndroid.widget.Toast; Public  class mainactivity extends Activity {    PrivateSensormanager Sensormanager;@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.main); Sensormanager = (Sensormanager) getsystemservice (Context.sensor_service);//access to servicesSensor sensor = Sensormanager. Getdefaultsensor (Sensor.type_accelerometer);//Sensor Type-acceleration sensorSensormanager.registerlistener (listener, sensor, sensormanager.sensor_delay_normal); }@Override    protected void OnDestroy() {if(Sensormanager! =NULL) {Sensormanager.unregisterlistener (listener); }Super. OnDestroy (); }/** * Sensor trigger * /    PrivateSensoreventlistener listener =NewSensoreventlistener () { Public void onsensorchanged(Sensorevent event) {floatXValue = Math.Abs (event.values[0]);//X-Axis direction acceleration            floatYvalue = Math.Abs (event.values[1]);//Y-Axis direction acceleration            floatZvalue = Math.Abs (event.values[2]);//z-axis direction acceleration            if(XValue > -|| Yvalue > -|| Zvalue > -) {//has an acceleration value greater than 20 in either direction, which triggers                /** * Here we just use toast as an example, you can write various methods in accordance with your own requirements;Toast.maketext (mainactivity. This,"You have activated the Shake function!" ", Toast.length_long). Show (); }        } Public void onaccuracychanged(Sensor sensor,intaccuracy) {}};}

Run the following example:


Summary :
1.getSystemService (Context.sensor_service);//Get Sensormanage object;
2.sensormanager.getdefaultsensor (sensor.type_accelerometer);//Get sensor type
3. This article just will shake a shake triggered as toast, there is a need to directly modify the method can be.

Favorite friends pay attention to me Oh! Thanks for your support!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Combat Simple Tutorial-the 31st gun (based on the acceleration sensor's shake-and-shake function example)

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.