2013-04-17 12 views
6

こんにちは、私は試してみてくださいと私はボタンをクリックして一度に場所を得ることができる、 私はそれを5分に一度表示する必要があります5分ごとに場所を取得するためのサンプルコードを与えることができます。gpsの場所を一度5分間アンドロイドに取得するには?

public void checkLocation(View v) { 

     //initialize location manager 
     manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     //check if GPS is enabled 
     //if not, notify user with a toast 
     if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      Toast.makeText(this, "GPS is disabled.", Toast.LENGTH_SHORT).show(); 
     } else { 

      //get a location provider from location manager 
      //empty criteria searches through all providers and returns the best one 
      String providerName = manager.getBestProvider(new Criteria(), true); 
      Location location = manager.getLastKnownLocation(providerName); 

      TextView tv = (TextView)findViewById(R.id.locationResults); 
      if (location != null) { 
       tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude"); 
      } else { 
       tv.setText("Last known location not found. Waiting for updated location..."); 
      } 
      //sign up to be notified of location updates every 15 seconds - for production code this should be at least a minute 
      manager.requestLocationUpdates(providerName, 15000, 1, this); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     TextView tv = (TextView)findViewById(R.id.locationResults); 
     if (location != null) { 
      tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude"); 
     } else { 
      tv.setText("Problem getting location"); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String arg0) {} 

    @Override 
    public void onProviderEnabled(String arg0) {} 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {} 

    // Find the closest Bart Station 
    public String findClosestBart(Location loc) { 
     double lat = loc.getLatitude(); 
     double lon = loc.getLongitude(); 

     double curStatLat = 0; 
     double curStatLon = 0; 
     double shortestDistSoFar = Double.POSITIVE_INFINITY; 
     double curDist; 
     String curStat = null; 
     String closestStat = null; 

     //sort through all the stations 
     // write some sort of for loop using the API. 

     curDist = Math.sqrt(((lat - curStatLat) * (lat - curStatLat)) + 
         ((lon - curStatLon) * (lon - curStatLon))); 
     if (curDist < shortestDistSoFar) { 
      closestStat = curStat; 
     } 

     return closestStat; 

     } 
+0

ハンドラ関数を使用し、5マイルごとに更新します。その関数にボタンを押したコード –

+0

ハンドラ関数を使う方法このコードを編集することができますpls –

答えて

10

も、私はすべての上の場所を取得するために実行可能なオブジェクトを使用している、場所を取得し、数分と距離に現在位置を取得するためにGPSのリスナーを設定するためのコードです数分。

Location gpslocation = null; 

private static final int GPS_TIME_INTERVAL = 60000; // get gps location every 1 min 
private static final int GPS_DISTANCE= 1000; // set the distance value in meter 

/* 
    for frequently getting current position then above object value set to 0 for both you will get continues location but it drown the battery 
*/ 

private void obtainLocation(){ 
if(locMan==null) 
    locMan = (LocationManager) getSystemService(LOCATION_SERVICE); 

    if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
     gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if(isLocationListener){ 
      locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
         GPS_TIME_INTERVAL, GPS_DISTANCE, GPSListener); 
       } 
      } 
     } 
} 

現在、このメソッドを使用して現在の位置を取得し、リスナーは距離が1分と1000メートルごとに呼び出されました。あなたがうまく設定期間時間にこの場所を取得するには、このハンドラおよび実行可能なを使用することができ、すべての5分取得するための

:ここ

private static final int HANDLER_DELAY = 1000*60*5; 

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
     public void run() { 
      myLocation = obtainLocation(); 
      handler.postDelayed(this, HANDLER_DELAY); 
     } 
    }, START_HANDLER_DELAY); 

は場所の変更イベントのためのGPSリスナーです:

private LocationListener GPSListener = new LocationListener(){ 
    public void onLocationChanged(Location location) { 
     // update location 
     locMan.removeUpdates(GPSListener); // remove this listener 
    } 

    public void onProviderDisabled(String provider) { 
    } 

    public void onProviderEnabled(String provider) { 
    } 

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

あなたはリスナーのインターバル時間とGPSの場所を取得するためのハンドラの時間を設定することができます。

1

私はこれを行うための実行可能な使用、

final Runnable r = new Runnable() { 
     public void run() { 
    //Here add your code location listener call 
    handler.postDelayed(this, 300000); 
     } 
    }; 

    handler.postDelayed(r, 300000); 
2

こんにちはは以下のタイマーコードを使用します。

が、これは私のコードであるあなたに

に感謝します。

あなたは携帯電話が100メートルを移動した場合、これは場所を取得します オプション1 以下のオプションを使用することができます。

captureFrequencey=3*60*1000; 
LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, captureFrequencey, 100, this); 

は、このリンクを見http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29

オプション2

TimerTask refresher; 
     // Initialization code in onCreate or similar: 
     timer = new Timer();  
     refresher = new TimerTask() { 
      public void run() { 
       handler.sendEmptyMessage(0); 
      }; 
     }; 
     // first event immediately, following after 1 seconds each 
     timer.scheduleAtFixedRate(refresher, 0,1000); 
     //======================================================= 


final Handler handler = new Handler() { 


     public void handleMessage(Message msg) { 
       switch (msg.what) { 
       case REFRESH: 
        //your code here 

        break; 
       default: 
        break; 
       } 
      } 
     }; 

を持っているタイマーは、あなたの継続時間(ご必要な時間への変更1000)のハンドラを呼び出します。

希望すると、これが役立ちます。

0

このようにしてみてください:

private Handler handler = new Handler(); 

handler.postDelayed(runnable, 300000); 

private Runnable runnable = new Runnable() { 
    public void run() { 
     if (location != null) { 

      onLocationChanged(location); 
     } else { 
      System.out.println("Location not avilable"); 
     } 

     handler.postDelayed(this, 300000); 
    } 
}; 
ここ
関連する問題