小弟最近研究接受GPS信号,需要手动得到GPS数据,在网上看见很多代码,我做了如下修改,但是数据无法更新。使用模拟器发送GPS数据,debug看到每次按键获得的都是最开始的老数据,没有更新,请达人们指教。
public class LocationTest extends Activity
{
LocationManager locManager;
EditText show;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
show = (EditText) findViewById(R.id.show);
// 从GPS获取最近的最近的定位信息
Location location = locManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
// 使用location根据EditText的显示,自定义函数
updateView(location);
mButton=(Button) findViewById(R.id.button1);
//做了个按键,手动收数据
mButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
//不是说这句话是接受最新数据吗??
Location location = locManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
updateView(location);
}
});
//下面是原有循环接受的代码,模拟器下测试可用
// 设置每3秒获取一次GPS的定位信息,移动8m一会运行
/*locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER
, 3000, 8, new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
// 当GPS定位信息发生改变时,更新位置
updateView(location);
} @Override
public void onProviderDisabled(String provider)
{
// GPS关闭,清空信息
updateView(null);
} @Override
public void onProviderEnabled(String provider)
{
// 当GPS LocationProvider可用时,更新位置
updateView(locManager
.getLastKnownLocation(provider));
} @Override
public void onStatusChanged(String provider, int status,
Bundle extras)
{
}
}); */
} // 更新EditText中显示的内容
public void updateView(Location newLocation)
{
if (newLocation != null)
{
StringBuilder sb = new StringBuilder();
sb.append("实时的位置信息:\n");
sb.append("经度:");
sb.append(newLocation.getLongitude());
sb.append("\n纬度:");
sb.append(newLocation.getLatitude());
}
}
}
public class LocationTest extends Activity
{
LocationManager locManager;
EditText show;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
show = (EditText) findViewById(R.id.show);
// 从GPS获取最近的最近的定位信息
Location location = locManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
// 使用location根据EditText的显示,自定义函数
updateView(location);
mButton=(Button) findViewById(R.id.button1);
//做了个按键,手动收数据
mButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
//不是说这句话是接受最新数据吗??
Location location = locManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
updateView(location);
}
});
//下面是原有循环接受的代码,模拟器下测试可用
// 设置每3秒获取一次GPS的定位信息,移动8m一会运行
/*locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER
, 3000, 8, new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
// 当GPS定位信息发生改变时,更新位置
updateView(location);
} @Override
public void onProviderDisabled(String provider)
{
// GPS关闭,清空信息
updateView(null);
} @Override
public void onProviderEnabled(String provider)
{
// 当GPS LocationProvider可用时,更新位置
updateView(locManager
.getLastKnownLocation(provider));
} @Override
public void onStatusChanged(String provider, int status,
Bundle extras)
{
}
}); */
} // 更新EditText中显示的内容
public void updateView(Location newLocation)
{
if (newLocation != null)
{
StringBuilder sb = new StringBuilder();
sb.append("实时的位置信息:\n");
sb.append("经度:");
sb.append(newLocation.getLongitude());
sb.append("\n纬度:");
sb.append(newLocation.getLatitude());
}
}
}
解决方案 »
- setPositiveButton()和setNegativeButton的用法
- monkeyrunner 真机测试失败
- 关于Eclipse搭建android开发平台问题
- 击碎屏幕这个小应用,大概是怎么实现的?求解
- 第一次启动初始化GridView对象,如何设置某一项的背景色?
- android onRestart问题
- surfaceView显示数据输出
- Android Build windows version SDK 问题?
- 想做个手机app,大家帮忙提些建议~thx
- 关于Android连接远程数据库的若干问题(内有详细描述)
- 如何在定义的对话框内,标题跟下面内容之间显示横线
- android socket编程IP地址问题
哦,谢谢2L,但是还有个问题,关于内存释放,如果我靠定时循环接受数据,然后更新UI用以下代码,是不是每次更新界面后,指向上次经纬度的string就释放了??,然后对新的数据开辟空间??public void updateView(Location newLocation)
{
if (newLocation != null)
{
DecimalFormat df = new DecimalFormat("###.00");
String temjing =new String();
String temwei =new String();
temjing=df.format(newLocation.getLongitude());
temwei=df.format(newLocation.getLatitude());
TextView1.setText(temjing);
TextView2.setText(temwei);
}
}