Android acceleration sensor simulates shaking effect-android learning Journey (66)

Source: Internet
Author: User

The main introduction to the Android Acceleration sensor simple usage, simulation shake, if the x, Y, z Three direction acceleration more than 15, will pop up toast, of course, you can set more complex strategies, such as the Judgment interval code as follows
 Public  class mainactivity extends Activity {    PrivateSensormanager Sensormanager;PrivateTextView TextView;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Sensormanager = (Sensormanager) getsystemservice (Context.sensor_service);        Sensor sensor = Sensormanager.getdefaultsensor (Sensor.type_accelerometer);    Sensormanager.registerlistener (Sensoreventlistener,sensor,sensormanager.sensor_delay_normal); }PrivateSensoreventlistener Sensoreventlistener =NewSensoreventlistener () {@Override         Public void onsensorchanged(Sensorevent sensorevent) {floatXValue = Math.Abs (sensorevent.values[0]);floatYvalue = Math.Abs (sensorevent.values[1]);floatZvalue = Math.Abs (sensorevent.values[2]);if(XValue > the|| Yvalue > the|| Zvalue > the) {Toast.maketext (mainactivity. This,"Shake it.", Toast.length_long). Show (); }        }@Override         Public void onaccuracychanged(Sensor sensor,inti) {}};@Override    protected void OnDestroy() {if(Sensormanager! =NULL) {Sensormanager.unregisterlistener (Sensoreventlistener); }Super. OnDestroy (); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.menu_main, menu);return true; }@Override     Public Boolean onoptionsitemselected(MenuItem Item) {//Handle Action Bar item clicks here. The Action Bar would        //automatically handle clicks on the Home/up button, so long        //As you specify a parent activity in Androidmanifest.xml.        intid = item.getitemid ();//noinspection simplifiableifstatement        if(id = = r.id.action_settings) {return true; }return Super. onoptionsitemselected (item); }}

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

Android acceleration sensor simulates shaking effect-android learning Journey (66)

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.