Linux教程網
Android獲取解析GPS數據:
- package title.navi.Location.Impl;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
-
-
- import android.content.Context;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.location.GpsStatus.NmeaListener;
- import android.location.LocationProvider;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
-
- public class NaviLocation {
-
- Context m_ctx;
- LocationManager m_locationManager;
- Location m_location;
- private boolean m_nmea_started = false;
-
- private NaviApp m_naviApp;
-
- class LocationDebug {
-
- }
-
- public NaviLocation(Context ctx, NaviApp naviApp) {
- m_ctx = ctx;
- m_naviApp = naviApp;
- }
-
- /**
- * 初始化
- *
- * @return
- */
- public int initialize() {
- // 創建LocationManger對象
- m_locationManager = (LocationManager) m_ctx
- .getSystemService(Context.LOCATION_SERVICE);
-
- // 獲取最後一次GPS定位的位置並上傳
- m_location = m_locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
-
- if( null != m_location )
- {
- UploadPos(m_location);
- }
-
-
- // 綁定監聽,有4個參數
- // 參數1,設備:有GPS_PROVIDER和NETWORK_PROVIDER兩種
- // 參數2,位置信息更新周期,單位毫秒
- // 參數3,位置變化最小距離:當位置距離變化超過此值時,將更新位置信息
- // 參數4,監聽
- // 備注:參數2和3,如果參數3不為0,則以參數3為准;參數3為0,則通過時間來定時更新;兩者為0,則隨時刷新
- // 1秒更新一次,或最小位移變化超過1米更新一次;
- // 注意:此處更新准確度非常低,推薦在service裡面啟動一個Thread
- // 在run中sleep(10000);然後執行handler.sendMessage(),更新位置
- m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
- 0, mLocationListener);
-
-
- return 0;
- }
-
- /**
- * 釋放
- *
- * @return
- */
- public int uninitialize() {
- return 0;
- }
-
- /**
- * 開始nmea接收標志
- */
- public void start_receive_nmea() {
- m_nmea_started = true;
- }
-
- // LocationListener,位置監聽,監聽位置變化,監聽設備開關與狀態
- LocationListener mLocationListener = new LocationListener() {
-
- /**
- * 位置信息變化時觸發
- */
- @Override
- public void onLocationChanged(Location location) {
-
- // 上傳當前位置
- if (location != null) {
- m_location = location;
- UploadPos(location);
- }
- }
-
- /**
- * GPS禁用時觸發
- */
- @Override
- public void onProviderDisabled(String provider) {
- }
-
- /**
- * GPS開啟時觸發
- */
- @Override
- public void onProviderEnabled(String provider) {
- // Location location=lm.getLastKnownLocation(provider);
- }
-
- /**
- * GPS狀態變化時觸發
- */
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- switch (status) {
- // GPS狀態為可見時
- case LocationProvider.AVAILABLE:
- Log.i("NavJava", "當前GPS狀態為可見狀態");
- break;
- // GPS狀態為服務區外時
- case LocationProvider.OUT_OF_SERVICE:
- Log.i("NavJava", "當前GPS狀態為服務區外狀態");
- break;
- // GPS狀態為暫停服務時
- case LocationProvider.TEMPORARILY_UNAVAILABLE:
- Log.i("NavJava", "當前GPS狀態為暫停服務狀態");
- break;
- }
- }
- };
-
- private boolean UploadPos(Location location)
- {
-
- return true;
- }
-
- private String getGPSTime(Location location)
- {
- String str = "0";
-
- Date date = new Date(location.getTime());
- SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmmss");
- str=dataFormat.format(date);
- return str;
- }
-
- }
Copyright ©
Linux教程網 All Rights Reserved