Android Sensor application: Shake a small example

Source: Internet
Author: User

public class Mainactivity extends Activity {
/**sensormanager Object */
Private Sensormanager Msensormanager;
/**vibrator Object */
Private vibrator Mvibrator;
/** defines a constant value */
private static final int sensor_value = 100;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
/** Get System Sense Manager service sensormanager*/
Msensormanager = (Sensormanager) getsystemservice (Sensor_service);
/** Get Vibration Service mvibrator*/
Mvibrator = (Vibrator) getsystemservice (Vibrator_service);
}

@Override
protected void Onresume () {
Super.onresume ();
/** register for monitoring in Onresume * *
if (Msensormanager! = null) {
Msensormanager.registerlistener (Msensoreventlistener, Msensormanager.getdefaultsensor (Sensor.TYPE_ACCELEROMETER ), Sensormanager.sensor_delay_normal);
}
}


@Override
protected void OnPause () {
Super.onpause ();
/** Unregister Listener Events */
if (Msensormanager! = null) {
Msensormanager.unregisterlistener (Msensoreventlistener);
}
}
/** Monitor mobile phone shaking listener implementation */
Private Sensoreventlistener Msensoreventlistener = new Sensoreventlistener () {
@Override
public void Onsensorchanged (Sensorevent event) {
/** sensor Data Changes */
/** get the sensing coordinates on the phone in three directions */
Float[] values = event.values;
float x = values[0];
Float y = values[1];
float z = values[2];
int sensorvalue = 20;
if (Math.Abs (x) > Sensorvalue | | Math.Abs (y) > Sensorvalue | | Math.Abs (z) > Sensorvalue) {
/** Vibration, Time 200 milliseconds */
Mvibrator.vibrate (200);
/**hanler send information out, wait for processing * *
Message msg = new Message ();
Msg.what = Sensor_value;
Handler.sendmessage (msg);
}
}
@Override
public void onaccuracychanged (Sensor arg0, int arg1) {
/**
* This means that the sensor accuracy is changed: In this case, the system calls the Onaccuracychanged () method, which provides the sensor object for which you want to reference a change in precision.
* Accuracy is represented by one of the following four state constants:
* Sensor_status_accuracy_low
* Sensor_status_accuracy_medium
* Sensor_status_accuracy_high
* sensor_status_unreliable
*/
}
};


@SuppressLint ("Handlerleak")
Handler Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Switch (msg.what) {
Case Sensor_value:
Toast.maketext (Mainactivity.this, "mobile phone shook ....", Toast.length_short). Show ();
Break
}
}


};


}

Android Sensor application: Shake a small example

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.