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);
老是为空。那里问了问题了。
解决方案 »
- 操作通讯录如何只返回姓名和手机号的cursor?能兼容各个版本
- 初学者的一些问题?
- Android 很纠结的问题,帮忙解答一下,谢谢!
- android 里可以用哪些填充GridView 的视图
- 如何在在自己开发的软件中运用谷歌地图?
- 有没有大神用IntelliJ IDEA开发Android的,请求问题,关于设置数据库(databases)的
- 如果文件不存在,使用chmod会出现错误吗
- 关于android 系统Calendar Provider源码中instances表格的问题
- 求android系统自带联系人源码一份
- andriod smack XMPP 怎么获取聊天室 的历史消息?
- 一个Android项目,Ubuntu中配置hudson,其中的ant的build.xml不会写,求教
- 请问手机锁屏界面的时间显示在哪里?在线等,帮顶给分.
把location声明全局变量应该就可以了