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
|
public class MainActivity extends Activity {
public static Context context;
public Button push = null;
public getGPS tache_getGPS = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplication().getApplicationContext();
push = (Button) findViewById(R.id.button);
push.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("T: onClick", "debut");
tache_getGPS = new getGPS();
tache_getGPS.execute();
Log.i("T: onClick", "fin");
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
class getGPS extends AsyncTask<Void, Integer, Location> implements
LocationListener {
final long REFRESH = 5 * 1000;
private Location location;
private LocationManager lm;
protected void onPreExecute() {
Log.i("T: onPreExcute", "debut");
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
// Configure location manager - I'm using just the network provider in
// this example
lm = (LocationManager) MainActivity.context
.getSystemService(Context.LOCATION_SERVICE);
String best = lm.getBestProvider(crit, false);
Log.i("T: onPreExecute", "best : " + best);
lm.requestLocationUpdates(best, 0, 1, this);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// nearProgress.setVisibility(View.VISIBLE);
Log.i("T: onPreExcute", "fin");
}
protected Location doInBackground(Void... params) {
Log.i("T: doInBackground", "debut");
// Try to use the last known position
Location lastLocation = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
/*
* // If it's too old, get a new one by location manager if
* (System.currentTimeMillis() - lastLocation.getTime() > REFRESH) {
* while (location == null) try { Thread.sleep(100); } catch (Exception
* ex) { }
*
* return location; }
*/
Log.i("T: doInBackground", "fin");
return lastLocation;
}
protected void onPostExecute(Location location) {
Log.i("T: onPostExecute", "debut");
// nearProgress.setVisibility(View.GONE);
lm = (LocationManager) MainActivity.context
.getSystemService(Context.LOCATION_SERVICE);
lm.removeUpdates(this);
Log.i("T: onPostExecute",
"Altitude : " + String.valueOf(location.getAltitude()));
Log.i("T: onPostExecute",
"Longitude : " + String.valueOf(location.getLongitude()));
Log.i("T: onPostExecute",
"Latitude : " + String.valueOf(location.getLatitude()));
Log.i("T: onPostExecute",
"Precision(mètre) : " + String.valueOf(location.getAccuracy()));
Log.i("T: onPostExecute", "fin");
Toast.makeText(
MainActivity.context,
"Altitude : " + String.valueOf(location.getAltitude()) + "\n"
+ "Longitude : "
+ String.valueOf(location.getLongitude()) + "\n"
+ "Latitude : "
+ String.valueOf(location.getLatitude()) + "\n"
+ "Precision(mètre) : "
+ String.valueOf(location.getAccuracy()),
Toast.LENGTH_SHORT).show();
return;
}
@Override
public void onLocationChanged(Location newLocation) {
Log.i("T: onLocationChanged", "debut");
location = newLocation;
Log.i("T: onLocationChanged", "fin");
}
@Override
public void onProviderDisabled(String provider) {
Log.i("T: onProviderDisabled", provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.i("T: onProviderEnabled", provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("T: onStatusChanged", "provider : " + provider);
Log.i("T: onStatusChanged", "status : " + status);
Log.i("T: onStatusChanged", "extras : " + extras.toString());
}
} |
Partager