2011-12-27 15 views
0

私はGPS Loggerを使用する際に助けが必要です。トグルボタンがあります。私が設定した場所では、私はGPSロガーを起動する必要があり、私はロガー間隔を設定する必要があります。 sqlite DBに緯度と経度を表示し、ログ間隔に基づいてDBを更新する必要があります。GPSロガーin android

実用的なサンプルコードは私にとって役に立ちます。私は多くのサンプルアプリケーションを見つけました。しかし、動作しませんでした。すべてのサンプル作業リンクは大きな助けになるでしょう。

答えて

2
public class LocalisationService extends Service 
{ 
    private LocationManager locationManager; 
    private BroadcastReceiver receiver; 

    //Define a listener that responds to location updates 
    private LocationListener locationListener = new LocationListener() 
    { 
     //Called when a new location is found by the network location provider 
     public void onLocationChanged(Location location) 
     { 
      //insertion of the location in the DB 
     } 

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

     public void onProviderEnabled(String provider) {} 

     public void onProviderDisabled(String provider) {} 
    }; 

    @Override 
    public IBinder onBind(Intent intent){return null;} 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     int interval = 1; //Specify the update interval 

     locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     // Register the listener with the Location Manager to receive location updates 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval, 0, locationListener); 

     IntentFilter filter = new IntentFilter(); 
     filter.addAction("STOP_LOCALISATION_SERVICE"); 

     receiver = new BroadcastReceiver() 
     { 
      @Override 
      public void onReceive(Context context, Intent intent) 
      { 
       stopSelf(); 
      } 
     }; 
     registerReceiver(receiver, filter); 
    } 

    @Override 
    public void onDestroy() 
    { 
     unregisterReceiver(receiver); 
     locationManager.removeUpdates(locationListener); 
     super.onDestroy(); 
    } 
}