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
| package com.kra8.gps;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
public class TransverseActivity extends Activity implements OnClickListener, LocationListener {
private LocationManager lManager;
private Location location;
private String choix_source = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
//On récupère le service de localisation
lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//Initialisation de l'écran
reinitialisationEcran();
//On affecte un écouteur d'évènement aux boutons
findViewById(R.id.choix_source).setOnClickListener(this);
findViewById(R.id.obtenir_position).setOnClickListener(this);
findViewById(R.id.insertion_BDD).setOnClickListener(this);
}
//Méthode déclencher au clique sur un bouton
public void onClick(View v) {
switch (v.getId()) {
case R.id.choix_source:
choisirSource();
break;
case R.id.obtenir_position:
obtenirPosition();
break;
case R.id.insertion_BDD:
try {
envoyerPosition();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
//Réinitialisation de l'écran
private void reinitialisationEcran(){
((TextView)findViewById(R.id.latitude)).setText("0.0");
((TextView)findViewById(R.id.longitude)).setText("0.0");
((TextView)findViewById(R.id.altitude)).setText("0.0");
((TextView)findViewById(R.id.vitesse)).setText("0.0");
findViewById(R.id.obtenir_position).setEnabled(false);
findViewById(R.id.insertion_BDD).setEnabled(false);
}
private void choisirSource() {
reinitialisationEcran();
//On demande au service la liste des sources disponibles.
List <String> providers = lManager.getProviders(true);
final String[] sources = new String[providers.size()];
int i =0;
//on stock le nom de ces source dans un tableau de string
for(String provider : providers)
sources[i++] = provider;
new AlertDialog.Builder( TransverseActivity.this)
.setItems(sources, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
findViewById(R.id.obtenir_position).setEnabled(true);
//on stock le choix de la source choisi
choix_source = sources[which];
//on ajoute dans la barre de titre de l'application le nom de la source utilisé
setTitle(String.format("%s - %s", getString(R.string.app_name),
choix_source)); }}).create().show();
}
private void obtenirPosition() {
//on démarre le cercle de chargement
setProgressBarIndeterminateVisibility(true);
//On demande au service de localisation de nous notifier tout changement de position
//sur la source (le provider) choisie, toute les minutes (60000millisecondes).
//Le paramètre this spécifie que notre classe implémente LocationListener et recevra
//les notifications.
lManager.requestLocationUpdates(choix_source, 60000, 0, this);
}
private void afficherLocation() {
//On affiche les informations de la position a l'écran
((TextView)findViewById(R.id.latitude)).setText(String.valueOf(location.getLatitude()));
((TextView)findViewById(R.id.longitude)).setText(String.valueOf(location.getLongitude()));
((TextView)findViewById(R.id.altitude)).setText(String.valueOf(location.getAltitude()));
((TextView)findViewById(R.id.vitesse)).setText(String.valueOf(location.getSpeed()));
}
private void envoyerPosition() throws ClientProtocolException, IOException{
HttpPost httppost = new HttpPost("http://10.0.2.2/dfghjk/WebForm1.aspx");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); //On crée la liste qui contiendra tous nos paramètres
//Et on y rajoute nos paramétres
nameValuePairs.add(new BasicNameValuePair("Altitude", String.valueOf(location.getAltitude())));
nameValuePairs.add(new BasicNameValuePair("Latitude", String.valueOf(location.getLatitude())));
nameValuePairs.add(new BasicNameValuePair("Longitude", String.valueOf(location.getLongitude())));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(httppost); //Voila, la requête est envoyée
HttpResponse response=httpclient.execute(httppost);
Log.e("http réponse",response.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String s = reader.readLine();
((TextView) findViewById(R.id.adresse)).setText("Données envoyées");
}
public void onLocationChanged(Location location) {
//Lorsque la position change...
Log.i("Tuto géolocalisation", "La position a changé.");
//... on stop le cercle de chargement
setProgressBarIndeterminateVisibility(false);
//... on active le bouton pour afficher l'adresse
findViewById(R.id.insertion_BDD).setEnabled(true);
//... on sauvegarde la position
this.location = location;
//... on l'affiche
afficherLocation();
//... et on spécifie au service que l'on ne souhaite plus avoir de mise à jour
lManager.removeUpdates(this);
}
public void onProviderDisabled(String provider) {
//Lorsque la source (GSP ou réseau GSM) est désactivé
Log.i("Tuto géolocalisation", "La source a été désactivé");
//...on affiche un Toast pour le signaler à l'utilisateur
Toast.makeText( TransverseActivity.this,
String.format("La source \"%s\" a été désactivé", provider),
Toast.LENGTH_SHORT).show();
//... et on spécifie au service que l'on ne souhaite plus avoir de mise à jour
lManager.removeUpdates(this);
//... on stop le cercle de chargement
setProgressBarIndeterminateVisibility(false);
}
public void onProviderEnabled(String provider) {
Log.i("Tuto géolocalisation", "La source a été activé.");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("Tuto géolocalisation", "Le statut de la source a changé.");
}
} |