You are on page 1of 3

Solución Ejercicio Voluntario Nº 2 .

Unidad Didáctica 3

Creamos un nuevo proyecto de Android y la activity SensorTest:.

Modificamos el archivo main.xml e insertamos el siguiente código:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent">
<TextView android:id= "@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Mueve el movil para ver un 'toast' y cambiar
el color de la pantalla" />
</LinearLayout>

Modificamos la clase SensorTest.java e insertamos el código siguiente:


package com.seas.Tema3Ejemplo2.sensor;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class SensorTest extends Activity implements


SensorEventListener {
private SensorManager sensorManager;
private boolean color = false;
private View view;
private long lastUpdate;

/** Called when the activity is first created. */

Solución Ejercicio Voluntario Programación Android Página 1 de 3


@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = findViewById(R.id.textView);
view.setBackgroundColor(Color.GREEN);

sensorManager = (SensorManager)
getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,

sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
lastUpdate = System.currentTimeMillis();
}

public void onSensorChanged(SensorEvent event) {


if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] values = event.values;
// movimientos
float x = values[0];
float y = values[1];
float z = values[2];

float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH *
SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
if (accelationSquareRoot >= 2) //
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
Toast.makeText(this, "El movil se ha movido ",
Toast.LENGTH_SHORT)
.show();
if (color) {
view.setBackgroundColor(Color.GREEN);

} else {
view.setBackgroundColor(Color.RED);
}
color = !color;
}

Solución Ejercicio Voluntario Programación Android Página 2 de 3


public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

@Override
protected void onResume() {
super.onResume();
// registra esta clase a la escucha de un cambio en la
orientacion y
// del acelerometro
sensorManager.registerListener(this,

sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
// paramos el listener para que solo lo realice la primera
vez
// sensorManager.unregisterListener(this);
// super.onStop();
}
}

Solución Ejercicio Voluntario Programación Android Página 3 de 3

You might also like