android gps

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 创建LocationManager对象

provider = locationManager.getBestProvider(getCriteria(), true); // 设置查询条件,返回定位方式,默认为GPS定位


//设置监听
locationListener = new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                Location l = locationManager.getLastKnownLocation(provider); // 获取位置信息
                updateView(l); // 更新EditText控件的内容
            }

            @Override
            public void onProviderDisabled(String provider) {
                
                updateView(null);
            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                updateView(location);
            }
        };



btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                progressDialog = new ProgressDialog(DemoActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progressDialog.setMessage("定位中!");
                progressDialog.show();
                new Thread() {
                    @Override
                    public void run() {
                        getLocation();
                    };
                }.start();

            }
        });

private void getLocation() {
        // 获取位置管理服务

        try {

            location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置

            address = gps.updateToNewLocation(location);

            handler.sendEmptyMessage(2);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            address = e.getMessage();
            handler.sendEmptyMessage(3);
        }
    }
private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                // 登录加载

                break;
            case 1:

                break;
            case 2:
                progressDialog.dismiss();
                updateView(location);

            default:
                break;
            }
        };
    };
复制代码

需要注意的地方:

1、重写onStart,检测GPS是否开启

View Code
复制代码
 1 @Override
 2     protected void onStart() {
 3         // TODO Auto-generated method stub
 4         super.onStart();
 5 
 6             Boolean a = locationManager
 7                 .isProviderEnabled(LocationManager.GPS_PROVIDER);
 8 
 9         if (!a) {
10             Intent intent = new Intent();
11             intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
12             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
13 
14             try {
15                 startActivity(intent);
16 
17             } catch (ActivityNotFoundException ex) {
18 
19                 // The Android SDK doc says that the location settings
20                 // activity
21                 // may not be found. In that case show the general settings.
22 
23                 // General settings activity
24                 intent.setAction(Settings.ACTION_SETTINGS);
25                 try {
26                     startActivity(intent);
27                 } catch (Exception e) {
28                 }
29             }
30         }
31 
32     }
复制代码

2、重写onResume,设置监听

View Code
复制代码
1 @Override
2     protected void onResume() {
3         // TODO Auto-generated method stub
4         super.onResume();
5 
6          locationManager.requestLocationUpdates(provider, 1000, 0,
7          locationListener);// 此处实际用network方式定位
8 
9     }
复制代码

2、重写onPause,注销监听

View Code
复制代码
1 @Override
2     protected void onPause() {
3         // TODO Auto-generated method stub
4         super.onPause();
5 
6          if (locationManager != null) {
7          locationManager.removeUpdates(locationListener);
8          }
9     }
复制代码

转载于:https://www.cnblogs.com/crazywenza/archive/2013/02/04/2891428.html

修复三星9100,、7100、9500......乃至sony等等智能手机的gps定位慢搜星慢的秒定gps修复, 方法来自互联网,亲自体验效果非常好:root后用re文件管理器替换自己手机里面的内容。位置在:根目录/system/etc下面。权限和原来相同。如果想自己修改可参照下面内容:打开RE管理器,在system下找到etc文件夹,再找到gps.conf(gps配置文件),将RE管理器挂载为读写,点击(长按)gps.conf文件,选择以文本编辑器方式打开,将里面内容修改为以下内容即可: NTP_SERVER=0.cn.pool.ntp.org NTP_SERVER=1.cn.pool.ntp.org NTP_SERVER=2.cn.pool.ntp.org NTP_SERVER=3.cn.pool.ntp.org NTP_SERVER=cn.pool.ntp.org XTRA_SERVER_1=http://xtra2.gpsonextra.net/xtra.bin XTRA_SERVER_2=http://xtra1.gpsonextra.net/xtra.bin XTRA_SERVER_3=http://xtra3.gpsonextra.net/xtra.bin SUPL_HOST=suplcn.sirf.com SUPL_HOST=221.176.0.55 SUPL_PORT=7275 更改完后,点击手机菜单键,保存并退出,重启手机,打开GPS,启动导航软件,10秒内定位!!! 真正有技术含量的内容 解释: NTP_SERVER=0.cn.pool.ntp.org(为中国0号授时子服务器,实时变动) NTP_SERVER=3.cn.pool.ntp.org(为中国3号授时子服务器,实时变动) NTP_SERVER=1.asia.pool.ntp.org(为亚洲1号授时子服务器,实时变动) NTP_SERVER=2.asia.pool.ntp.org(为亚洲2号授时子服务器,实时变动) SUPL_HOST=221.176.0.55(中国移动定位服务器) SUPL_HOST=suplcn.sirf.com(上海电信定位服务器)也可改为:supl.google.com(谷歌定位服务器)或supl.nokia.com(诺基亚定位服务器) SUPL_PORT=7275 (服务器端口;也可用7276) 每天中国对时服务器变动信息可查看网址:http://www.pool.ntp.org/zone/cn(如追求极致,可根据实际变动调整) 1、授时服务器,也就是NTP Server,对于搜星后快速定位很重要。有的时候搜到的卫星很多但是定位很慢,就是因为和授时服务器连接不顺畅,快速和NTP Server同步,有助于提高GPS定位速度。建议:优先使用国内的NTP Server。 NTP全称是Network Time Protocol,是用来让计算机之间实现时间同步的协议,而发布这种校对时间的服务器,就是NTP Server。我们的操作系统XP、win7上都会用这个东西对时间。 2、supl服务器,没有统一的译名,似乎也没有统一的标准,主要是通过网络提供GPS卫星星历图,提高搜星速度;另外通过信号基站等其他辅助手段提高定位精度。建议:要提高速度,依然是尽量使用国内服务器 3、以下部分为服务器地址,身在国内的用户优先考虑的 国内的NTP Server: 0.cn.pool.ntp.org 1.cn.pool.ntp.org 2.cn.pool.ntp.org 3.cn.pool.ntp.org cn.pool.ntp.org 这是一组服务器集群,服务器位于上海电信和浙江台州电信,经实践证明这些网址是等效的。任意一个网址都可能对应这两地中的一个,台州电信和上海电信这两组服务器的ping值都在30~50ms左右(有线宽带和GSM网络均测试了)。 ntp.api.bz 这个是一组NTP服务器集群,目前有6台服务器,位于上海电信,速度很快 210.72.145.44 中国国家授时中心服务器IP,在西安,狗日的禁ping,但是实际上速度飞快 AGPS supl服务器地址: suplcn.sirf.com 114.80.208.5:7275 上海电信 221.176.0.55:7275 北京移动机房,中国移动的SUPL服务器,没查到域名,速度很快,建议移动用户选择 sls1.sirf.com 66
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值