Skip to content

Commit 596d561

Browse files
committed
Sensor tutorial corrections
1 parent 617dd8d commit 596d561

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

‎de.vogella.android.sensor/AndroidManifest.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
android:versionName="1.0">
66
<application android:icon="@drawable/icon" android:label="@string/app_name">
77
<activity android:name=".SensorTest"
8-
android:label="@string/app_name">
8+
android:label="@string/app_name" android:configChanges="orientation">
99
<intent-filter>
1010
<action android:name="android.intent.action.MAIN" />
1111
<category android:name="android.intent.category.LAUNCHER" />

‎de.vogella.android.sensor/default.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
# project structure.
99

1010
# Project target.
11-
target=android-9
11+
target=android-10
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:orientation="vertical"
4-
android:layout_width="fill_parent"
5-
android:layout_height="fill_parent"
6-
>
7-
<TextView
8-
android:layout_width="fill_parent"
9-
android:layout_height="wrap_content"
10-
android:text="@string/hello"
11-
/>
3+
android:orientation="vertical" android:layout_width="fill_parent"
4+
android:layout_height="fill_parent">
5+
<TextView android:id="@+id/textView" android:layout_width="match_parent"
6+
android:layout_height="match_parent" android:text="Shake to get a toast" />
127
</LinearLayout>

‎de.vogella.android.sensor/src/de/vogella/android/sensor/SensorTest.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
package de.vogella.android.sensor;
22

33
import android.app.Activity;
4+
import android.graphics.Color;
45
import android.hardware.Sensor;
56
import android.hardware.SensorEvent;
67
import android.hardware.SensorEventListener;
78
import android.hardware.SensorManager;
89
import android.os.Bundle;
10+
import android.view.View;
11+
import android.view.Window;
12+
import android.view.WindowManager;
913
import android.widget.Toast;
1014

1115
public 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

Comments
 (0)