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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
package com.pIndus.sensors;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class Sensors extends Activity implements SensorEventListener{
SensorManager sm;
LocationManager lm;
LocationListener ls;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean accelSupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST);
if(!accelSupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
((TextView)findViewById(R.id.acc)).setText("Accéléromètre non disponible");
}
boolean compassSupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
if(!compassSupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION));
((TextView)findViewById(R.id.compass)).setText("Boussole non disponible");
}
boolean proxySupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_FASTEST);
if(!proxySupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY));
((TextView)findViewById(R.id.proxy)).setText("Capteur de proximité non disponible");
}
ls = new MyLocationListener();
lm.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, ls);
}
@Override
public void onStop()
{
super.onStop();
lm.removeUpdates(ls);
sm.unregisterListener(this);
}
@Override
public void onPause()
{
super.onStop();
lm.removeUpdates(ls);
sm.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event)
{
switch(event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
onAccelChanged(event);
break;
case Sensor.TYPE_ORIENTATION:
onCompassChanged(event);
break;
case Sensor.TYPE_PROXIMITY:
onProxyChanged(event);
break;
}
}
public void onAccelChanged(SensorEvent event)
{
float aX,aY,aZ;
aX = event.values[0];
aY = event.values[1];
aZ = event.values[2];
((TextView)findViewById(R.id.axeX)).setText("Axe X : " + aX);
((TextView)findViewById(R.id.axeY)).setText("Axe Y : " + aY);
((TextView)findViewById(R.id.axeZ)).setText("Axe Z : " + aZ);
}
public void onCompassChanged(SensorEvent event)
{
float azimuth,pitch,roll;
azimuth = event.values[0];
pitch = event.values[1];
roll = event.values[2];
((TextView)findViewById(R.id.azimuth)).setText("Azimuth : " + azimuth);
((TextView)findViewById(R.id.pitch)).setText("Pitch : " + pitch);
((TextView)findViewById(R.id.roll)).setText("Roll : " + roll);
}
public void onProxyChanged(SensorEvent event)
{
float x;
x = event.values[0];
((TextView)findViewById(R.id.prox)).setText("Proximité : " + x);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
double longitude, lattitude;
longitude = location.getLongitude();
lattitude = location.getLatitude();
((TextView)findViewById(R.id.longi)).setText("Longitude : " + longitude);
((TextView)findViewById(R.id.latti)).setText("Lattitude : " + lattitude);
}
@Override
public void onProviderDisabled(String provider) {
((TextView)findViewById(R.id.warnGPS)).setText("GPS Desactivé");
}
@Override
public void onProviderEnabled(String provider) {
((TextView)findViewById(R.id.warnGPS)).setText("GPS Activé");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
} |
Partager