嗨 我想更新一個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