-
Notifications
You must be signed in to change notification settings - Fork 252
/
Accelerometer.txt
82 lines (74 loc) · 2.93 KB
/
Accelerometer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//permission
<uses-permission android:name="android.permission.VIBRATE" />
//ui
<TextView
android:id="@+id/accelerometer_text"
android:layout_width="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:text="TEXT"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="30dp" />
//code
[Activity(Label = "Sensors", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : Activity, ISensorEventListener
{
static readonly object _syncLock = new object();
SensorManager _sensorManager;
TextView _sensorTextView;
float last_x=0;
float last_y=0;
float last_z=0;
long last_Time=0;
float ThreadShould = 3000; //mini speed
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
_sensorManager = (SensorManager)GetSystemService(Context.SensorService);
_sensorTextView = FindViewById<TextView>(Resource.Id.accelerometer_text);
}
protected override void OnResume()
{
base.OnResume();
_sensorManager.RegisterListener(this,
_sensorManager.GetDefaultSensor(SensorType.Accelerometer),
SensorDelay.Ui);
}
protected override void OnPause()
{
base.OnPause();
_sensorManager.UnregisterListener(this);
}
public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy)
{
// We don't want to do anything here.
}
public void OnSensorChanged(SensorEvent e)
{
lock (_syncLock)
{
float x= e.Values[0];
float y= e.Values[1];
float z= e.Values[2];
long Current_Time= DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; ;
if((Current_Time-last_Time)>100)
{
long different = Current_Time - last_Time;
last_Time = Current_Time;
float speed = Math.Abs((x + y + z) - (last_x + last_y + last_z)) / different * 10000;
if (speed > ThreadShould)
{
_sensorTextView.Text = string.Format("newShouck={0:f}",speed);
Vibrator vibrator = (Vibrator) GetSystemService(Context.VibratorService);
vibrator.Vibrate(100);
}
last_x = x;
last_y = y;
last_z = z;
}
}
}
}