嗨 我想更新一个textview来通知用户GPS坐标已被记录。 我的问题是,我需要从另一个类访问位置信息,我不能直接编辑除了onCreate类之外的任何东西的textview。Android TextView更新
这里是我的代码:
public class Location extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
private final String TAG = null;
@Override
public void onLocationChanged(android.location.Location location) {
// TODO Auto-generated method stub
location.getLatitude();
location.getLongitude();
String Text = "My current location is: " + "Latitud = " + location.getLatitude() + "Longitud = " + location.getLongitude();
try
{
File root = Environment.getExternalStorageDirectory();
File gps = new File(root, "log.txt");
BufferedWriter out = new BufferedWriter(
new FileWriter(gps,true)) ;
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(Text);
out.write(Text);
out.write("");
out.close();
}
catch (IOException e) {
Log.e(TAG, "Could not write file " + e.getMessage());
}
}
感谢
你的意思是创建一个静态的String文本并在onCreate类中使用它来更新TextView? – Nekro 2011-03-15 18:17:38