2011-12-28 42 views
0

的GPS記錄器值到sqlite的這是我的GPSLoggerservice代碼:獲取的Android

package com.mygps.android; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.channels.FileLock; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.List; 
import java.util.Timer; 
import java.util.TimerTask; 
import com.mygps.android.AppSettings; 
import com.mygps.android.AppLog; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 

import android.location.LocationManager; 

import android.os.Bundle; 

import android.os.Environment; 

import android.os.IBinder; 

import android.util.Log; 

import android.view.View; 

import android.widget.TextView; 

import android.widget.Toast; 



public class GPSLoggerService extends Service implements LocationListener { 

    // this is a hack and need to be changed, here the offset is the length of 

    // the tag XML "</Document></kml", 

    // we minus this offset from the end of the file and write the next 

    // Placemark entry. 

    private static final int KML_INSERT_OFFSET = 17; 



    private static final int gpsMinTime = 500; 

    private static final int gpsMinDistance = 0; 

    private static final int TIMER_DELAY = 1000; 

    private static final int GEOCODER_MAX_RESULTS = 5; 



    TextView abc; 

    TextView abcd; 



    private LocationManager manager = null; 

    private double latitude = 0.0; 

    private double longitude = 0.0; 

    private double altitude = 0.0; 

    private Timer monitoringTimer = null; 



    public GPSLoggerService() { 

     AppLog.logString("GPSLoggerService.GPSLoggerService()."); 

    } 



    @Override 

    public IBinder onBind(Intent arg0) { 

     AppLog.logString("GPSLoggerService.onBind()."); 



     return null; 

    } 



    @Override 

    public void onCreate() { 

     AppLog.logString("GPSLoggerService.onCreate()."); 



     super.onCreate(); 

    } 



    @Override 

    public void onStart(Intent intent, int startId) { 

     AppLog.logString("GPSLoggerService.onStart()."); 



     startLoggingService(); 

     startMonitoringTimer(); 



     super.onStart(intent, startId); 

    } 



    public int onStartCommand(Intent intent, int flags, int startId) { 

     AppLog.logString("GPSLoggerService.onStartCommand()."); 



     startLoggingService(); 

     startMonitoringTimer(); 



     return Service.START_STICKY; 

    } 



    @Override 

    public void onLocationChanged(Location location) { 

     AppLog.logString("GPSLoggerService.onLocationChanged()."); 



     latitude = location.getLatitude(); 

     longitude = location.getLongitude(); 

     altitude = location.getAltitude(); 

    } 



    @Override 

    public void onProviderDisabled(String provider) { 

     AppLog.logString("GPSLoggerService.onProviderDisabled()."); 

    } 



    @Override 

    public void onProviderEnabled(String provider) { 

     AppLog.logString("GPSLoggerService.onProviderEnabled()."); 

    } 



    @Override 

    public void onStatusChanged(String provider, int status, Bundle extras) { 

     AppLog.logString("GPSLoggerService.onStatusChanged()."); 

    } 



    private void startLoggingService() { 

     if (manager == null) { 

      manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     } 



     final Criteria criteria = new Criteria(); 



     criteria.setAccuracy(Criteria.ACCURACY_FINE); 

     criteria.setAltitudeRequired(false); 

     criteria.setBearingRequired(false); 

     criteria.setCostAllowed(true); 

     criteria.setPowerRequirement(Criteria.POWER_LOW); 



     final String bestProvider = manager.getBestProvider(criteria, true); 



     if (bestProvider != null && bestProvider.length() > 0) { 

      manager.requestLocationUpdates(bestProvider, gpsMinTime, 

        gpsMinDistance, this); 

     } else { 

      final List<String> providers = manager.getProviders(true); 



      for (final String provider : providers) { 

       manager.requestLocationUpdates(provider, gpsMinTime, 

         gpsMinDistance, this); 

      } 

     } 

    } 



    private void stopLoggingService() { 

     stopSelf(); 

    } 



    private void startMonitoringTimer() { 

     monitoringTimer = new Timer(); 



     monitoringTimer.scheduleAtFixedRate(new TimerTask() { 

      @Override 

      public void run() { 

       if (longitude != 0.0 && latitude != 0.0) { 

        monitoringTimer.cancel(); 

        monitoringTimer = null; 



        manager.removeUpdates(GPSLoggerService.this); 



        saveCoordinates(latitude, longitude, altitude, 

          getLocationName(latitude, longitude)); 

        stopLoggingService(); 

       } 

      } 

     }, GPSLoggerService.TIMER_DELAY, GPSLoggerService.TIMER_DELAY); 

    } 



    private String getLocationName(double latitude, double longiture) { 

     String name = ""; 

     Geocoder geocoder = new Geocoder(this); 



     try { 

      List<Address> address = geocoder.getFromLocation(latitude, 

        longiture, GPSLoggerService.GEOCODER_MAX_RESULTS); 



      name = address.get(0).toString(); 

     } catch (IOException e) { 

      e.printStackTrace(); 

     } 



     return name; 

    } 



    private void saveCoordinates(double latitude, double longitude, 

      double altitude, String name) { 

     File folder = new File(Environment.getExternalStorageDirectory(), 

       "GPSLogger"); 

     boolean isNew = false; 



     if (!folder.exists()) { 

      folder.mkdirs(); 



      isNew = true; 

     } 



     try { 

      File kmlFile = new File(folder.getPath(), 

        AppSettings.getLogFileName(this)); 



      if (!kmlFile.exists()) { 

       kmlFile.createNewFile(); 



       isNew = true; 

      } 



      if (isNew) { 

       FileOutputStream initialWriter = new FileOutputStream(kmlFile, 

         true); 



       String xml = "<?xml version=\"1.0\"?>" 

         + "<kml xmlns=\"http://www.opengis.net/kml/2.2\">" 

         + "<Document>" + "</Document>" + "</kml>"; 



       initialWriter.write(xml.getBytes()); 

       initialWriter.flush(); 

       initialWriter.close(); 

      } 



      SimpleDateFormat sdf = new SimpleDateFormat(

        "yyyy-MM-dd'T'HH:mm:ssZ"); 

      String dateString = sdf.format(new Date()); 



      String placemark = "<Placemark>" + "<name>" + name + "</name>" 

        + "<description>" 

        + "Created by GPSLogger sample application" 

        + "</description>" + "<TimeStamp>" + "<when>" + dateString 

        + "</when>" + "</TimeStamp>" + "<Point>" + "<coordinates>" 

        + String.valueOf(longitude) + "," 

        + String.valueOf(latitude) + "," + String.valueOf(altitude) 

        + "</coordinates>" + "</Point>" + "</Placemark>" 

        + "</Document>" + "</kml>"; 





      String a = String.valueOf(longitude); 

      String b = String.valueOf(latitude); 



//   abc = (TextView)findViewById(R.id.lat); 

//   abcd = (TextView)findViewById(R.id.longi); 

//   

//   abc.setText(a); 

//   abcd.setText(b); 



     // Toast.makeText(this, "a=" + a + "b=" + b , Toast.LENGTH_LONG).show(); 
    AppLog.logString("abcdef" + a); 

      //Log.i("Longi", a); 


      RandomAccessFile fileAccess = new RandomAccessFile(kmlFile, "rw"); 
      FileLock lock = fileAccess.getChannel().lock(); 

      fileAccess.seek((kmlFile.length() - GPSLoggerService.KML_INSERT_OFFSET)); 

      fileAccess.write(placemark.getBytes()); 
      lock.release(); 

      fileAccess.close(); 

     } catch (IOException e) { 

      e.printStackTrace(); 

     } 

    } 

    private TextView findViewById(int lat) { 

     // TODO Auto-generated method stub 

     return null; 

    } 

} 

輸出我DDMS數據/數據/ com.xyz.abc得到的是:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?> 
<map> 
<int name="loggingInterval" value="15" /> 
<string name="logFile">GPSLog.2011-12-28 15:38:05.kml</string> 
<boolean name="isServiceRunning" value="true" /> 
</map> 

如何我可以讀取該值並獲取數據

+0

你是什麼意思「這個值」?和哪些「數據」? – 2011-12-28 10:23:55

+0

有問題嗎?如果是,則提供logcat輸出。 – 2011-12-28 10:24:16

回答

0

它看起來像在文件系統上保存xml文檔(一個空的KML文件),然後將座標保存爲xml格式的字符串,然後將其插入到kml文檔中。最後,您將文件名保存在另一個xml文檔中。如果你想這樣做,那麼你需要閱讀你的kml文件(GPSLog.2011-12-28 15:38:05.kml)並解析它,以便你可以提取你在那裏寫的座標和任何其他數據。 但是,如果你想使用SqlLite,你可以創建一個帶有單獨字段的表來保存座標和其他數據,或者你可以簡單地將你的'地標'字符串存儲在數據庫中(使用合適的_id鍵,這樣你可以正確檢索數據),然後將其解析爲GeoPoint對象或您自己的擴展對象(如果有更多數據要存儲)。 我個人用的是經度,緯度,高度等單獨的列。

+0

謝謝。我也在尋找相同的解決方案。我只需要從位置管理器獲取2個值的經緯度,並將其添加到我的sqlite數據庫。我還需要根據時間間隔更新GEOLogger數據。任何示例代碼鏈接都會對我有很大的幫助。 – 2011-12-28 11:44:50

+0

下面是獲取Sqllite數據庫的鏈接:http://stackoverflow.com/questions/4402948/android-database/4403142#4403142 – 2011-12-28 13:26:19