11package de .vogella .android .sensor ;
22
33import android .app .Activity ;
4+ import android .graphics .Color ;
45import android .hardware .Sensor ;
56import android .hardware .SensorEvent ;
67import android .hardware .SensorEventListener ;
78import android .hardware .SensorManager ;
89import android .os .Bundle ;
10+ import android .view .View ;
11+ import android .view .Window ;
12+ import android .view .WindowManager ;
913import android .widget .Toast ;
1014
1115public class SensorTest extends Activity implements SensorEventListener {
1216 private SensorManager sensorManager ;
1317 private float lastX = 0 ;
1418 private float lastY = 0 ;
1519 private float lastZ = 0 ;
20+ private View view ;
1621
1722 /** Called when the activity is first created. */
1823 @ Override
1924 public void onCreate (Bundle savedInstanceState ) {
25+ requestWindowFeature (Window .FEATURE_NO_TITLE );
26+ getWindow ().setFlags (WindowManager .LayoutParams .FLAG_FULLSCREEN ,
27+ WindowManager .LayoutParams .FLAG_FULLSCREEN );
28+
2029 super .onCreate (savedInstanceState );
2130 setContentView (R .layout .main );
31+ view = findViewById (R .id .textView );
32+ view .setBackgroundColor (Color .GREEN );
33+
2234 sensorManager = (SensorManager ) getSystemService (SENSOR_SERVICE );
2335 sensorManager .registerListener (this ,
2436 sensorManager .getDefaultSensor (Sensor .TYPE_ACCELEROMETER ),
@@ -27,7 +39,7 @@ public void onCreate(Bundle savedInstanceState) {
2739
2840 @ Override
2941 public void onSensorChanged (SensorEvent event ) {
30- if (event .sensor .getType () == SensorManager . SENSOR_ACCELEROMETER ) {
42+ if (event .sensor .getType () == Sensor . TYPE_ACCELEROMETER ) {
3143 float [] values = event .values ;
3244 // Movement
3345 float x = values [0 ];
@@ -36,9 +48,13 @@ public void onSensorChanged(SensorEvent event) {
3648
3749 float accelationSquareRoot = (x * x + y * y + z * z )
3850 / (SensorManager .GRAVITY_EARTH * SensorManager .GRAVITY_EARTH );
39- if (accelationSquareRoot >= 4 ) // twice the accelation of earth
51+ if (accelationSquareRoot >= 2 ) //
52+ {
4053 Toast .makeText (this , "Device was shuffed" , Toast .LENGTH_SHORT )
4154 .show ();
55+ view .setBackgroundColor (Color .RED );
56+ }
57+
4258 }
4359
4460 }
0 commit comments