Android 模拟手机GPS入门

 

1. 设置GPS活动的许可权限. 在AndroidMainfest.xml文件中添加

    

 

2. 创建活动的布局. 一个Button,点击按钮获取位置经纬度, 四个TextView , 布局如下:

  配置main.xml文件:

 

两个LinearLayout嵌套到一个LinerLayout中, 注意属性layout_width和layout_height的变化.

 

3. 编写GPS活动, 用到的关键类LocationManager, Criteria, 关键方法requestLocationUpdates(,,,,)(方法说明:

Registers the current activity to be notified periodically by the named provider. Periodically, the supplied LocationListener will be called with the current Location or with status updates.

It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method.

In case the provider is disabled by the user, updates will stop, and the onProviderDisabled(String) method will be called. As soon as the provider is enabled again, the onProviderEnabled(String) method will be called and location updates will start again.)

 

代码AndroidLBS.java

 

4. 运行活动. 在模拟器中直接运行活动的话, 显然不会显示当前位置坐标, 我们可以用DDMS(Dalvik Debug Monitor Server), 打开Eclipse中Window->Open Perspective->DDMS , 如下图,输入坐标, 点击 Send

 

在此过程中, 坐标始终无法显示, 终于找到原因. 首先确保你的模拟器支持GPS功能, 如果不支持的话, 要把GPS功能添加进模拟器. 如下图:

一切设置完毕, 运行活动,点击按钮, 还是无法显示发送的坐标, 试着修改模拟器中的Target, 把Target 由

Android 2.3.3 -API Level 10 修改为 Google APIs -API Level 10后, 运行OK, 但是纬度小数点部分精度有时跟发送的不一样, 还不知道是什么原因.

 

PS: 模拟器屏幕尺寸有以下几种(VGA 640*480):

WVGA800
WQVGA400
WQVGA432
HVGA: 480*320
WVGA854
QVGA: 240×320

WQVGA(Wide Quarter Video Graphics Array) 表示 480X272(宽高比16:9)或者400X240(宽高比5:3)

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值