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
|
package com.MiniProjet;
import android.app.Activity;
import android.content.Context;
//import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class Mini_Projet extends Activity {
private SensorManager SenMan;
private TextView TextViewX;
private TextView TextViewY;
private TextView TextViewZ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//objet SenMan qui prend la main
SenMan = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
//Sensor.TYPE_ACCELEROMETER défini le type de capteur
//SensorManager.SENSOR_DELAY_NORMAL est le delai de rafraichissement des infos
SenMan.registerListener(SenListener, SenMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
//Définition des objets de la fenêtre xml
TextViewX = (TextView) findViewById(R.id.valueX);
TextViewY = (TextView) findViewById(R.id.valueY);
TextViewZ = (TextView) findViewById(R.id.valueZ);
}
//Méthode qui met à jour les champs du xml
public void Position(int iX, int iY, int iZ)
{
TextViewZ.setText(" "+iZ);
TextViewX.setText(" "+iX);
TextViewY.setText(" "+iY);
}
//Objet qui défini l'action à réaliser quand le capteur bouge
private final SensorEventListener SenListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent se)
{
int x = (int) se.values[0];
int y = (int) se.values[1];
int z = (int) se.values[2];
Position(x, y , z);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
@Override
protected void onResume()
{
super.onResume();
SenMan.registerListener(SenListener, SenMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop()
{
SenMan.unregisterListener(SenListener);
super.onStop();
}
} |
Partager