public class GpsActivity extends Activity {
LocationManager lm;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.gps);
tv =(TextView) this.findViewById(R.id.tv1);
lm= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> proviers = lm.getAllProviders();
for (String string : proviers) {
System.out.println(string);
}
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateLocation(location);
}
private void updateLocation(Location location){
if(location!=null){
StringBuffer sb = new StringBuffer();
sb.append("当前位置信息\n");
sb.append("经度:").append(location.getLongitude())
.append("\n纬度:").append(location.getLatitude())
.append("\n高度:").append(location.getAltitude())
.append("\n方向:").append(location.getBearing());
tv.setText(sb.toString());
System.out.println(sb.toString());
}else{
tv.setText("没有获取到信息 ");
}
}
}权限我也加上了。为什么Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
老是为空。那里问了问题了。
解决方案 »
- bat脚本问题
- 小白求救关于intent.setClass的第二个参数~~~~~%>_<%~~~~~
- 请教Android 开发ANT+Wireless怎么在模拟器上运行程序
- Android 系统软件如何调用自带短信程序发送短信(求高手指点
- 蓝牙问题,求解
- [求助]android里如何判断手机的sim卡类型
- 请教,如何调整ListView的每一行的行高?
- Android中怎么解析html字符串或页面
- android客户端与服务器交互,传的数据是JSON格式,请问用webservice好呢还是http好呢?
- java.lang.IllegalStateException: eglMakeCurrent failed EGL_BAD_ALLOC谁遇到过这个异常?
- 一个Android项目,Ubuntu中配置hudson,其中的ant的build.xml不会写,求教
- 请问手机锁屏界面的时间显示在哪里?在线等,帮顶给分.
把location声明全局变量应该就可以了