Android for shake-shake function

Source: Internet
Author: User
Tags random sentences

Have done a small project, which realized the shaking of the function and scraping a scraping function and playing background music, etc.

Shake-shake function is mainly used for acceleration sensor :sensor.type_accelerometer

shake a shake function The main is to monitor the mobile phone acceleration changes, when the setting of a certain value, that is, trigger event .


/** * FileName:MainActivity.java * @desc This class is used to display a shake-shake interface * @author HTP * @Date 20140311 * @version 1.00 */public class M Ainactivity extends Activity implements Onclicklistener {private tabhost tabhost;private Musicnamedao dao;//Private Imag Ebutton bt_setmusic;//Sound Private MediaPlayer mediaplayer;//show Shake a shake appears private TextView tv;//content to be displayed on TV private String msentence;//through the class sentence can get a shake to display the content of private sentence sentence;//display label "Scrape" private TextView tv_rubbler;// Gravity Sensor Manager Private Sensormanager sensormanager;//Vibration Control Private Vibrator vibrator;//label private static final String tag = "Mainac Tivity ";p rivate static final int sensor_shake = 10;//private Intent musicintent = new Intent (" Com.angel.Android.MUSIC "); * * Called when the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);d ao = new Musicnamedao (this); Uri Musicuri = Uri.parse ("android.resource://com.example.romance/raw/" + Dao.getnamE ()); MediaPlayer = Mediaplayer.create (this, Musicuri);//toast.maketext (Mainactivity.this, Musicuri.tostring (), 1). Show (); Mediaplayer.start ();//Play Music mediaplayer.setlooping (TRUE);//set Loop DAO = new Musicnamedao (this);//StartService ( musicintent); Tv_rubbler = (TextView) Findviewbyid (R.id.tv_rubber); Tv_rubbler.setonclicklistener (this);//Bt_ Setmusic.setonclicklistener (this); sentence = new sentence ();//Uri Musicuri = Uri.parse ("/sdcard/ttpod/song/Rainbow. mp3") ///Get the music path under res/raw/TV = (TextView) Findviewbyid (r.id.tv_text);//Associate the TextView space to display 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, and the third parameter value gets the frequency of the sensor information}}@ overrideprotected void OnPause () {super.onpause (); if (SenSormanager! = null) {//Cancel 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 = 18;if (Math.Abs (x) > Medumvalue && math.abs (y) > medumvalue| | Math.Abs (y) > Medumvalue && math.abs (z) > medumvalue| | Math.Abs (x) > 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 () {@Ove rridepublic void Handlemessage (Message msg) {suPer.handlemessage (msg); switch (msg.what) {Case sensor_shake:msentence = sentence.getsentence ();//Get random sentences// Toast.maketext (Mainactivity.this, Msentence, 1). Show (); Tv.settext (msentence);//The resulting sentence is displayed on the TextView//LOG.I (TAG, " Shake detected, perform operation! "); break;}}; @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); Mediaplayer.stop ();//Stop Music on exit }/** * Keyboard event, when the back button is pressed to ask whether to press the exit program again *///exit time private long exittime = 0; @Overridepublic boolean onKeyDown (int keycode, Keye Vent event) {if (keycode = = keyevent.keycode_back&& event.getaction () = = Keyevent.action_down) {if ( System.currenttimemillis ()-Exittime) > {toast.maketext (Getapplicationcontext (), "Press again to exit the program", Toast.length_ Short). Show (); exittime = System.currenttimemillis ();} else {finish (); System.exit (0);} return true;} Return Super.onkeydown (KeyCode, event);} /** * Button Event listener */@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {///When the button is pressed, it will switch to the scratch Scraping page case R.id.tv_rubber: {Toast. Maketext (Mainactivity.this, "scrape", 1). Show (); Intent rubblerintent = new Intent (mainactivity.this,rubbleract.class); StartActivity (rubblerintent); break;} Default:break;}}}




Android for shake-shake function

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.