Android sensor; Shake a lottery function

Source: Internet
Author: User
Tags float max

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
Private string[] allresults={"She Loves Me", "She Doesn't 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);
Invoking the Sensor service
Sensormanager manager= (Sensormanager) Getsystemservice (Sensor_service);
Building Sensor objects
Sensor s=manager.getdefaultsensor (Sensor.type_accelerometer);//Accelerator object, and other very many objects
Set up monitoring, when the value in the sensor changes, the self-active monitoring feedback
Manager.registerlistener (New Sensoreventlistener () {

@Override
public void Onsensorchanged (Sensorevent event) {
if (flag) {
if (starttime==-1) {
Starttime=system.currenttimemillis ();
}
When there is a change, run this method on your own initiative
The three-direction acceleration value can be obtained by the event
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) {
Min=values[0];
}
if (Max > && min <-10) {
Flag = false;
Long X=system.currenttimemillis ();
if (x-starttime>2000) {


Toast.maketext (Mainactivity.this, "timeout, please shake Again",
Toast.length_short). Show ();
}
else {
We can make a result.

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");

}
});
}


}

Android sensor; Shake a lottery function

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.