Android-like micro-letter shake-and-shake function realization idea and code _android

Source: Internet
Author: User
Tags abs stub
Copy Code code 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;
Vibration
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 shaking ...");
}
}
}
@Override
protected void Onresume () {
TODO auto-generated Method Stub
Super.onresume ();
Msensormanager.registerlistener (This, msensormanager.getdefaultsensor (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 ();
}
}
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.