I'm trying to make a simple activity that reads the GPS position of the user and actualices a simple textview everytime the position of the user changes.
I find some examples en google but all of them are not nice examples, because only capture the position of the user ONE time, and i need that the textview get's actualiced everytime the position changes with the new latitude and longitude of the user.
I tryed to do a thread but it fails and i think it is not necesary to do a thread, im in the wrong way.
Code examples are welcome
EDIT: i'm adding the solution proposed by the user NickT. This solution fails. I dont know why but only actualizes two times the textview, with the two first GPS values that i pass to the emulator with DDMS.... after this the thextview isn't g开发者_如何转开发etting actualiced more times... ¿why?. I make a breakpoint in onLocationChanged, and it only get's called the first two times i send a gps positiones... but never more. ¿what is happening?
public class GpsMiniActivity extends Activity implements LocationListener{
private LocationManager mLocMgr;
private TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout rl = new FrameLayout(this.getApplicationContext());
LinearLayout ll= new LinearLayout(this.getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(rl);
rl.addView(ll);
tv1=new TextView(getApplicationContext());
ll.addView(tv1);
//setContentView(R.layout.main);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 0, this);
}
@Override
public void onLocationChanged(Location location) {
tv1.setText("Lat " + location.getLatitude() + " Long " + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
Absolute minimum example:
public class GpsMiniActivity extends Activity implements LocationListener{
private LocationManager mLocMgr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 0, this);
}
@Override
public void onLocationChanged(Location location) {
TextView tv = (TextView) findViewById(R.id.tv1);
tv.setText("Lat " + location.getLatitude() + " Long " + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
You'll also need permission ACCESS_FINE_LOCATION in your manifest and a Textview id tv1 in main.xml
精彩评论