views:

44

answers:

1

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);       
}

}

A: 

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

TuomasR
Thx for answer. I have tried make app according familiar tutorial (using requestLocationUpdates). But the problem is the same. When i try to substitite GPS position to emulator it works only once. Im trying to change position many times but position didnt change. I dont know what wrong. :/
Tom