Android sensor; shake and draw

Source: Internet
Author: User

Package com. kane. sensortest;
Import java. util. Random;
Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorManager;
Import android. OS. Bundle;
Import android. app. Activity;


Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;


Import android. widget. Toast;


Public class MainActivity extends Activity {
Private TextView content;
Private Button btn;
// Draw lots
Private String [] allResults = {"She loves me", "she does not love me", "she loves him "};

Private Random random = new Random ();

Private boolean flag = false;
Private long startTime =-1;
// X acceleration in the initial horizontal direction
Private float max = 0;
Private float min = 0;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Content = (TextView) findViewById (R. id. content );
Btn = (Button) findViewById (R. id. btn );
// Call the sensor Service
SensorManager manager = (SensorManager) getSystemService (SENSOR_SERVICE );
// Create a Sensor Object
Sensor s = manager. getdefasensensor (Sensor. TYPE_ACCELEROMETER); // accelerator object, there are many other objects
// Set the listener. When the value in the sensor changes, the listener automatically sends feedback.
Manager. registerListener (new SensorEventListener (){

@ Override
Public void onSensorChanged (SensorEvent event ){
If (flag ){
If (startTime =-1 ){
StartTime = System. currentTimeMillis ();
}
// This method is automatically executed when there is a change
// You can use event to obtain acceleration values in three directions.
Float [] values = event. values;
/* StringBuilder builder = new StringBuilder ();
Builder. append (values [0] + "\ r \ n ");
Builder. append (values [1] + "\ r \ n ");
Builder. append (values [2] + "\ r \ n ");
Content. setText (builder );*/
If (values [0]> max ){
Max = values [0];
}
If (values [0] Min = values [0];
}
If (max> 10 & min <-10 ){
Flag = false;
Long x = System. currentTimeMillis ();
If (x-startTime & gt; 2000 ){


Toast. makeText (MainActivity. this, "timeout, please shake again ",
Toast. LENGTH_SHORT). show ();
}
Else {
// The result is displayed.

// Randomly generated
Int index = random. nextInt (3 );
Content. setText ("Result:" + allResults [index]);

}
StartTime =-1;

}

}

}

@ Override
Public void onAccuracyChanged (Sensor sensor, int accuracy ){

}
}, S, 0 );

Btn. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Flag = true;
Content. setText ("please start shaking ");

}
});
}


}

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.