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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| package com.projet.first.myapplication;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
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.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
private SensorManager sensorManager;
private Sensor magnet;
private double latitude;
private double longitude;
private double altitude;
private float accuracy;
private float x,y,z;
public void ecrire(String texte) throws IOException {
TextView total = (TextView)findViewById(R.id.all);
total.setText(texte);
setContentView(total);
try
{
File myFile = new File(Environment.getExternalStorageDirectory() +
File.separator + "appli_test","testAppli.txt"); //on déclare notre futur fichier
File myDir = new File(Environment.getExternalStorageDirectory() +
File.separator + "appli_test"); //pour créer le repertoire dans lequel on va mettre notre fichier
Boolean success=true;
if (!myDir.exists()) {
success = myDir.mkdir(); //On crée le répertoire (s'il n'existe pas!!)
}
if (success){
String data= texte;
FileOutputStream output = new FileOutputStream(myFile,true); //le true est pour écrire en fin de fichier, et non l'écraser
output.write(data.getBytes());
}
else {
Log.e("TEST1","ERROR DE CREATION DE DOSSIER");
}
}
catch(IOException ioe){System.out.println("erreur : " + ioe );}
//on "catch" l exception ici si il y en a une, et on l affiche sur la console
}
public String construireChaine(double longitude, double latitude, double altitude, float accuracy, float x, float y, float z) {
return "x: " + x + " y: " + y + " z: " + z + " lat: " + latitude + " longi: " + longitude + " alti: " + altitude + " accuracy: " + accuracy;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
magnet = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
File f = new File("/sdcard/toto");
if (!f.exists()) {
f.mkdirs();
}
File myFile = new File("sdcard" +
File.separator + "Android"+ File.separator +"appli_test","testAppli.txt"); //on déclare notre futur fichier
File myDir = new File("sdcard" +
File.separator + "Android"+ File.separator +"appli_test"); //pour créer le repertoire dans lequel on va mettre notre fichier
Boolean success=true;
if (!myDir.exists()) {
success = myDir.mkdir(); //On crée le répertoire (s'il n'existe pas!!)
}
if (success){
String data= "tesssssst";
try (FileOutputStream output = new FileOutputStream(myFile, true)) {
output.write(data.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else {
Log.e("TEST1","ERROR DE CREATION DE DOSSIER");
}
TextView tv = new TextView(this);
tv.setText((CharSequence) myDir.getAbsolutePath());
setContentView(tv);
}
@Override
public void onResume() {
super.onResume();
//Obtention de la référence du service
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
//Si le GPS est disponible, on s'y abonne
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
abonnementGPS();
}
}
@Override
public void onPause() {
super.onPause();
//On appelle la méthode pour se désabonner
desabonnementGPS();
}
/**
* Méthode permettant de s'abonner à la localisation par GPS.
*/
public void abonnementGPS() {
//On s'abonne
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);
}
/**
* Méthode permettant de se désabonner de la localisation par GPS.
*/
public void desabonnementGPS() {
//Si le GPS est disponible, on s'y abonne
locationManager.removeUpdates(this);
}
final SensorEventListener mSensorEventListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Que faire en cas de changement de précision ?
}
public void onSensorChanged(SensorEvent sensorEvent) {
String chaineAEcrire = null;
if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
x = sensorEvent.values[0];
y = sensorEvent.values[1];
z = sensorEvent.values[2];
chaineAEcrire = construireChaine(longitude,latitude,altitude,accuracy,x,y,z);
}
try {
ecrire(chaineAEcrire);
} catch (IOException e) {
e.printStackTrace();
}
}
};
@Override
public void onLocationChanged(final Location location) {
//On affiche dans un Toat la nouvelle Localisation
final StringBuilder msg = new StringBuilder("lat : ");
msg.append(location.getLatitude());
msg.append( "; lng : ");
msg.append(location.getLongitude());
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
accuracy = location.getAccuracy();
String messageAEcrire = construireChaine(longitude,latitude,altitude,accuracy,x,y,z);
try {
ecrire(messageAEcrire);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, msg.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(final String provider) {
//Si le GPS est désactivé on se désabonne
if("gps".equals(provider)) {
desabonnementGPS();
}
}
@Override
public void onProviderEnabled(final String provider) {
//Si le GPS est activé on s'abonne
if("gps".equals(provider)) {
abonnementGPS();
}
}
@Override
public void onStatusChanged(final String provider, final int status, final Bundle extras) { }
} |
Partager