Reputation: 83
Hello i have app which ask every 5second for GPS position but it allways returns same position. I have tried substitite position in DDMS or by telnet (geo fix ... ...) But allway it return initial postion. Whats wrong?
public class App09_GPS_RepeatedAsking extends Activity {
TextView tv1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.textview);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
Handler mHandler = new Handler();
@Override
public void run() {
try{
mHandler.post(new Runnable(){
@Override
public void run() {
LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider =lm.getBestProvider(criteria, false);
//Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location loc = lm.getLastKnownLocation(provider);
String Text = "My current location is: " + "Latitud = " + loc.getLatitude() + " Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
Log.v("LOG",Text);
}
);
}catch(Exception e){
Log.v("LOG",e.toString());
}
}
}, 0, 5000);
}
}
Upvotes: 1
Views: 617
Reputation: 11
It's said that changing emulator time to the real time maybe effective. You can have a try
Upvotes: 1
Reputation: 2316
To request a GPS location in an even interval, you should use the LocationManager.requestLocationUpdates() -method:
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)
There's a tutorial on this on the Android Developer site as well: http://developer.android.com/guide/topics/location/obtaining-user-location.html#Updates
Upvotes: 1