devdbi

Sensor Acelerômetro Android

May 4th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. package br.com.paulosouzacc.tp2nicelerometer;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.hardware.Sensor;
  6. import android.hardware.SensorEvent;
  7. import android.hardware.SensorEventListener;
  8. import android.hardware.SensorManager;
  9. import android.location.Location;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.widget.TextView;
  13.  
  14. import java.text.SimpleDateFormat;
  15. import java.util.Calendar;
  16. import java.util.Date;
  17.  
  18. public class MainActivity extends AppCompatActivity implements SensorEventListener {
  19.     // atributos relacionados ao acelerometro
  20.     private SensorManager mSensorManager;
  21.     private Sensor mSensor;
  22.     // atributos para marcar a maior aceleracao nos eixos x, y e z
  23.     private float bestOfX = 0, bestOfY = 0, bestOfZ = 0;
  24.     // marcador do tempo para zerar a marcacao das maiores aceleracoes
  25.     private long time;
  26.     // INULTILIZADO
  27.     // Marcacao de quantas vezes o celular foi balancado
  28.     private int state = 0;
  29.     // atributo com o minimo de aceleracao que o celular deve ter em algum eixo
  30.     // para passar de estado
  31.     private int min = -15;
  32.  
  33.     Location mLastLocation;
  34.  
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_main);
  39.  
  40.  
  41.         // Inicia o acelerometro
  42.         mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  43.         mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  44.         mSensorManager.registerListener((SensorEventListener) this, mSensor, mSensorManager.SENSOR_DELAY_FASTEST);
  45.  
  46.     }
  47.  
  48.     @Override
  49.     protected void onResume() {
  50.         super.onResume();
  51.         mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
  52.     }
  53.  
  54.     @Override
  55.     protected void onPause() {
  56.         bestOfX = bestOfY = bestOfZ = 0;
  57.         super.onPause();
  58.         mSensorManager.unregisterListener(this);
  59.     }
  60.  
  61.     @Override
  62.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
  63.     }
  64.  
  65.     @Override
  66.     public void onSensorChanged(SensorEvent event) {
  67.         // x = [0], y = [1], z = [2]
  68.         float x, y, z;
  69.         float sumBestOfXYZ;
  70.         long now = System.currentTimeMillis();
  71.         int diff;
  72.  
  73.         TextView tv = (TextView) findViewById(R.id.linear_acceleration);
  74.  
  75.         x = event.values[0];
  76.         y = event.values[1];
  77.         z = event.values[2];
  78.         tv.setText(String.format("%s, %s, %s", x, y, z));
  79.  
  80.         if (x < bestOfX)
  81.             bestOfX = x;
  82.         if (y < bestOfY)
  83.             bestOfY = y;
  84.         if (z < bestOfZ)
  85.             bestOfZ = z;
  86.  
  87.  
  88.         diff = (int) (now - time);
  89.  
  90.         if (x > bestOfX || y > bestOfY || z > bestOfZ && ((x < min || y < min || z < min))) {
  91.             state = 1;
  92.         }
  93.  
  94.         // passa para o estado 1
  95.         // estado 1 = a pessoa balancou o celular uma vez no espaco de tempo determinado
  96.         if (bestOfX < min || bestOfY < min || bestOfZ < min && state == 0) {
  97.             //time = now;
  98.             // zera os marcadores dos sensores
  99.             bestOfX = 0;
  100.             bestOfY = 0;
  101.             bestOfZ = 0;
  102.             setDayOrNight();
  103.         }
  104.             /*
  105.             Tentativa de pegar a segunda vez que balancou o celular
  106.             mSensorManager.unregisterListener(this);
  107.             try {
  108.                 Thread.sleep((long)500);
  109.             } catch (InterruptedException e) {
  110.                 e.printStackTrace();
  111.             }
  112.             mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
  113.             xyz[0] = event.values[0];
  114.             xyz[1] = event.values[1];
  115.             xyz[2] = event.values[2];
  116.             if (xyz[0] > min || xyz[1] > min || xyz[2] > min){
  117.                 setDayOrNight();
  118.             }*/
  119.         // retorna para o estado 0 se nao obteve acereracao no tempo de 1 segundo
  120.         if (diff > 1000) {
  121.             state = 0;
  122.             bestOfX = 0;
  123.             bestOfY = 0;
  124.             bestOfZ = 0;
  125.         }
  126.     }
  127.  
  128.  
  129.     protected void setDayOrNight() {
  130.         try {
  131.             // ferramenta para formatar a hora com hh:mm:ss
  132.             SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  133.             // pega a data atual sem formatacao
  134.             Date data = new Date();
  135.             int i = (int) (Math.random() * 10);
  136.             // instancia o calendario
  137.             Calendar cal = Calendar.getInstance();
  138.             // seta a data no calendario instanciado
  139.             cal.setTime(data);
  140.             // formata a data atual no formato hh:mm:ss
  141.             Date dataAtual = cal.getTime();
  142.             String horaAtual = sdf.format(dataAtual);
  143.             // inteiro para comparacao
  144.             int hora;
  145.             // Text view que ira' aparecer se e' dia ou noite
  146.             TextView tv;
  147.             tv = (TextView) findViewById(R.id.day_or_night);
  148.  
  149.             if (horaAtual.length() > 0) {
  150.                 hora = Integer.parseInt(horaAtual.replace(":", ""));
  151.                 // so' se for dia
  152.                 if (hora > 60000 && hora < 180000) {
  153.                     tv.setText(getString(R.string.bom_dia));
  154.                 }
  155.                 // se for noite
  156.                 else {
  157.                     tv.setText(getString(R.string.boa_noite));
  158.                 }
  159.                 // numero aleatorio so' para testes
  160.                 tv = (TextView) findViewById(R.id.description);
  161.                 tv.setText("Aleatório: " + i);
  162.             }
  163.  
  164.         } catch (Exception e) {
  165.             e.printStackTrace();
  166.         }
  167.     }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment