Android is similar to WeChat's shake function implementation ideas and code

Source: Internet
Author: User

Copy codeThe Code is as follows: package com. eboy. testyaoyiyao;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import android. app. Activity;
Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorManager;
Import android. OS. Bundle;
Import android. OS. Vibrator;
Import android. view. Menu;
Import android. widget. TextView;
Public class MainActivity extends Activity implements SensorEventListener {
// Sensor Manager
Private SensorManager mSensorManager = null;
// Vibrate
Private Vibrator mVibrator = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );
MVibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
Public void onAccuracyChanged (Sensor arg0, int arg1 ){
// TODO Auto-generated method stub
}
Public void onSensorChanged (SensorEvent arg0 ){
// TODO Auto-generated method stub
Int sensorType = arg0.sensor. getType ();
Float [] values = arg0.values;
If (sensorType = Sensor. TYPE_ACCELEROMETER ){
If (Math. abs (values [0])> 14 | Math. abs (values [1])> 14 | Math. abs (values [2])> 14 ){
MVibrator. vibrate (100 );
TextView tv1 = (TextView) findViewById (R. id. textView1 );
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss E ");
Tv1.setText (f. format (new Date () + "Mobile Phone shake ...");
}
}
}
@ Override
Protected void onResume (){
// TODO Auto-generated method stub
Super. onResume ();
MSensorManager. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER), SensorManager. SENSOR_DELAY_NORMAL );
}
@ Override
Protected void onStop (){
// TODO Auto-generated method stub
MSensorManager. unregisterListener (this );
Super. onStop ();
}
@ Override
Protected void onPause (){
// TODO Auto-generated method stub
MSensorManager. unregisterListener (this );
Super. onPause ();
}
}

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.