Android手機GPS定位功能的簡單應用講解,Android手機的GPS定位功能在Android開發中很常見,是最基本的應用功能,大多數智能手機都帶有GPS芯片,硬件底層和軟件驅動就省瞭,我們隻管調y用GPS相應的函數接口就能獲取經緯度。明確一下基本功能:實現每隔2秒獲取一次GPS經緯度,用累加的方式計算行程,並保存到SQLite數據中。
首先,獲取經緯度的方法為lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,1,locationListener);其中LocationManager.GPS_PROVIDER為本位所采用的位置提供其,locationListener為位置監聽方法。locationListener方法實現具體細節如下:
private final LocationListener locationListener = new LocationListener(){ @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub updateWithNewLocation(location); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub updateWithNewLocation(null); } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }; private void updateWithNewLocation(Location location) { if (location != null) { String time = String.valueOf(Utils.formatDate(location.getTime())); double lat = location.getLatitude(); double lng = location.getLongitude(); String battery = Constants.currLevel+"%"; if(N>2){ count+=MyMethod.getDistanceFromXtoY(lat, lng, relat, relon); relon=lng; relat=lat; } else { relon =lng; relat =lat; N++; } db.insertGPS(time, battery, lat, lng, count); Intent intent=new Intent(); intent.putExtra("time", time); intent.putExtra("count", count); intent.setAction("android.intent.action.test"); sendBroadcast(intent); }else{ } }