Beh, è possibile utilizzare Timer e TimerTask classi.
LocationManager manager;
TimerTask mTimertask;
GPSLocationListener mGPSLocationListener;
int i = 0; //Here i works as counter;
private static final int MAX_ATTEMPTS = 250;
public void getCurrentLocation() {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mGPSLocationListener = new GPSLocationListener();
manager.addGpsStatusListener(mGPSStatusListener);
mTimerTask = new LocTimerTask(LocationManager.GPS_PROVIDER);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.v(TAG, "GPS ENABLED");
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
50.0f, mGPSLocationListener);
} else {
turnGPSOn();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
50.0f, mGPSLocationListener);
}
if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L,
50.0f, mNetworkLocationListener);
}
if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.v(TAG, "GPS ENABLED");
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000L, 50.0f, mGPSLocationListener);
}
myLocTimer = new Timer("LocationRunner", true);
myLocTimer.schedule(mTimerTask, 0, 500);
}
GPSStatusListener
private GpsStatus.Listener mGPSStatusListener = new GpsStatus.Listener() {
@Override
public synchronized void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Log.v(TAG, "GPS SAtellitestatus");
GpsStatus status = manager.getGpsStatus(null);
mSattelites = 0;
Iterable<GpsSatellite> list = status.getSatellites();
for (GpsSatellite satellite : list) {
if (satellite.usedInFix()) {
mSattelites++;
}
}
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
/*
* Toast.makeText(getApplicationContext(), "Got First Fix",
* Toast.LENGTH_LONG).show();
*/
break;
case GpsStatus.GPS_EVENT_STARTED:
/*
* Toast.makeText(getApplicationContext(), "GPS Event Started",
* Toast.LENGTH_LONG).show();
*/
break;
case GpsStatus.GPS_EVENT_STOPPED:
/*
* Toast.makeText(getApplicationContext(), "GPS Event Stopped",
* Toast.LENGTH_LONG).show();
*/
break;
default:
break;
}
}
};
LocationListener
public class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location argLocation) {
location = argLocation;
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
classe TimerTask
class LocTimerTask extends TimerTask {
String provider;
public LocTimerTask(String provider) {
this.provider = provider;
}
final Handler mHandler = new Handler(Looper.getMainLooper());
Runnable r = new Runnable() {
@Override
public void run() {
i++;
Log.v(TAG, "Timer Task run" + i);
location = manager.getLastKnownLocation(provider);
if (location != null) {
Log.v(TAG, "in timer task run in if location not null");
isGPS = true;
onLocationReceived(location);
myLocTimer.cancel();
myLocTimer.purge();
mTimerTask.cancel();
return;
} else {
Log.v(TAG, "in timer task run in else location null");
isGPS = false;
if (location == null && i == MAX_ATTEMPTS) {
Log.v(TAG, "if 1 max attempts done");
turnGPSOff();
location = manager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Log.v(TAG,
"if 1 max attempts done Location from network not null");
Log.v(TAG,
"if 1 max attempts done Location from network not null coordinates not null");
onLocationReceived(location);
myLocTimer.cancel();
myLocTimer.purge();
mTimerTask.cancel();
return;
}
} else {
return;
}
}
i = 0;
}
};
public void run() {
mHandler.post(r);
}
}
Qui il timer è stato programmato per essere eseguito ogni 500 millisecondi. Significa che ogni 500 millisecondi verrà eseguito il task run method
del timer. Nel metodo run provare a ottenere la posizione dal provider GPS per no specifico. di tentativi (Qui MAX_ATTEMPTS) diciamo 5 o 10. Se si ottiene la posizione all'interno specificato no. dei tentativi quindi utilizzare quella posizione se il contatore (Qui i) ha superato MAX_ATTEMPTS, quindi ottenere la posizione dal Provider di rete. per ottenere la posizione, avevo passato quella posizione al metodo di richiamata onLocationReceived(Location mLoc)
in cui puoi continuare a lavorare con i dati sulla posizione. Ecco come si intende utilizzare il metodo di callback:
Listener
public interface OnLocationReceivedListener {
public void onLocationReceived(Location mLoc); //callback method which will be defined in your class.
}
La classe deve implementare l'ascoltatore sopra definito.Nella tua classe:
@Override
public void onLocationReceived(Location mLoc) {
//Do your stuff
}
Speranza che aiuta. Se qualcuno ha un approccio migliore, per favore fatemelo sapere.
Dai un'occhiata a questo libretto: https://code.google.com/p/little-fluffy-location-library/ – dannyroa
Hai estratto il vecchio post 1. http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatic-in-android 2. http://stackoverflow.com/questions/3145089/questo-è-il-simplest-and-most- robusto-per-ottenere-gli-utenti-corrente-posizione-in-a/3145655 # 3145655 – surhidamatya
Il modo più semplice è usare una libreria di livello superiore come questa: https://github.com/delight-im/Android -SimpleLocation – caw