1

私は私のデータベースに私の場所を送信したい..私はこのような場所(経度と緯度)を取得するためのコードを持っています

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);

    //Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(null);

    locationManager.requestLocationUpdates(provider, 2000, 10,
                                           locationListener);

}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };
  private void updateWithNewLocation(Location location) {
        //EditText etlat=(EditText)findViewById(R.id.lat);
        //EditText etlong=(EditText)findViewById(R.id.lng);


        if (location != null) {
            double lat = location.getLatitude();
            System.out.println(lat);
             double lng = location.getLongitude();
             System.out.println(lng);
             double alt = location.getAltitude();
             System.out.println(alt);
             etlat.setText(String.valueOf(lat));
             etlng.setText(String.valueOf(lng));
             etalt.setText(String.valueOf(alt));
        } 
  }

そして、私は自分の場所を10分間隔でデータベースに保存したい..
どうすればこれを行うことができますか??ありがとう:)

4

3 に答える 3

2
 locationManager.requestLocationUpdates(provider,(10*60*1000),10,
                                           locationListener);

これを試すことができます。

于 2012-05-07T09:00:42.857 に答える
1

Service を拡張するクラスを記述し、適切なタグをマニフェスト ファイルに追加します。

onCreate() メソッドで位置情報を取得してデータベースに保存するなど、サービス実行時に実行したいコードを記述します。

このチュートリアルを参照できます: http://www.vogella.com/articles/AndroidServices/article.html

于 2012-05-23T05:28:36.387 に答える
0

10分間隔でタイマーを開始するだけです..そしてmake requestLocationUpdate();

次に、各 GPS フィックスの緯度と対数の値のセットを取得します。location.getAccuracy() によって最適な値を取得し、データベース挿入用のコードを記述します。

于 2012-05-07T08:53:36.090 に答える