android gps 定位

來源:互聯網
上載者:User

客戶需要跟蹤運單資訊,決定採用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 settings20                 // activity21                 // may not be found. In that case show the general settings.22 23                 // General settings activity24                 intent.setAction(Settings.ACTION_SETTINGS);25                 try {26                     startActivity(intent);27                 } catch (Exception e) {28                 }29             }30         }31 32     }

2、重寫onResume,設定監聽

View Code

1 @Override2     protected void onResume() {3         // TODO Auto-generated method stub4         super.onResume();5 6          locationManager.requestLocationUpdates(provider, 1000, 0,7          locationListener);// 此處實際用network方式定位8 9     }

2、重寫onPause,登出監聽

View Code

1 @Override2     protected void onPause() {3         // TODO Auto-generated method stub4         super.onPause();5 6          if (locationManager != null) {7          locationManager.removeUpdates(locationListener);8          }9     }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.