• android gps 定位


    客户需要跟踪运单信息,决定采用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     }
  • 相关阅读:
    qt中文乱码问题
    【Qt开发】QThread 实用技巧、误区----但文档中没有提到
    TCP/UDP网络性能测试工具
    wireshark怎么抓包、wireshark抓包详细图文教程
    INDEX--从数据存放的角度看索引2
    杂谈--从基数评估来看问题1
    INDEX--从数据存放的角度看索引
    疑难杂症--为数据库主文件所在磁盘保留一定量磁盘空间
    统计--VARCHAR与NVARCHAR在统计预估上的区别
    杂谈--SQL SERVER版本
  • 原文地址:https://www.cnblogs.com/yc_huangxiao/p/2877708.html
Copyright © 2020-2023  润新知