2013-02-01 5 views
-2

위치 서비스에 this link을 사용하고 있습니다.5 분마다 위치를 얻는 방법?

이제 5 분마다 위치를 가져 오는 함수를 호출하는 BackgroundService를 만들고 싶습니다. 나는 이것을 위해 Timer를 사용할 필요가 있다고 생각한다.이 위치 클래스 사이에이 5 분 간격을 관리하는 방법을 알려줘.

+1

사용 캡은 당신의 질문이 될 악화 잠급니다. – Eli

+0

이봐, 젠장, 내가 여기에 새로운 답변을 해주십시오 .......... 만약 가만히 있으면 –

+0

이런 종류의 질문 Stackoverflow에 오신 것을 환영합니다. FAQ를 묻는 질문을 읽어주십시오 – Eli

답변

0

사용이 :

Timer timer = new Timer(); 
       timer.schedule(new TimerTask() { 
        public void run() { 
//your code to get lat long 

        } 
       }, 0, 500000); 
+0

각하 께서이 귀중한 회신을 보내 주셔서 감사합니다 ....이 전화가 계속됩니다. –

+0

타이머 타이머 = 새 타이머(); timer.schedule (새의 TimerTask() { 공공 무효 실행() { // 위도 긴 얻을 수있는 코드 \t \t을 locationManager = (을 locationManager) getSystemService (Context.LOCATION_SERVICE) \t \t \t \t locationListener = 새로운 GPSLocationListenere(); \t \t \t \t locationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER 1000 * 120,5, locationListener),} }, 0, 500000); } –

1
public class LocationService extends Service {  
    private Timer timer; 
    private long UPDATE_INTERVAL ; 
    public static final String Stub = null; 
    LocationManager mlocmag; 
    LocationListener mlocList ; 
    private double lat,longn; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     webService = new WebService(); 
     mlocmag = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     mlocList = new MyLocationList(); 

     Location loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (loc == null) { 
      loc = mlocmag.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     } 
     timer = new Timer();  // location. 
     UpdateWithNewLocation(loc); // This method is used to get updated 
     mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocList); 
    } 

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



    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (timer != null) { 
      timer.cancel(); 
     } 
     mlocmag.removeUpdates(mlocList); 
    } 

    @Override 
    public boolean stopService(Intent name) { 
     return super.stopService(name); 
    } 

    private void UpdateWithNewLocation(final Location loc) { 
     final SharedPreferences prefs = getSharedPreferences(Const.COMMON_SHARED, Context.MODE_PRIVATE); 
     userId = prefs.getString(Const.COMMON_USERID, null); 
     gps = prefs.getInt(Const.COMMON_GPS, 0); 

     UPDATE_INTERVAL = 500000; 

     timer.scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
     if (loc != null) { 
      final double latitude = loc.getLatitude(); // Updated lat 
      final double longitude = loc.getLongitude(); // Updated long 

      String response = null ; 
       if (lat != latitude || longn != longitude) { 

        response = webService.updateLatandLong(userId, latitude, longitude); 
        lat = latitude; 
        longn = longitude; 

       } 
     } 

     else { 
      String latLongStr = "No lat and longitude found"; 
     } 

    } 
     }, 0, UPDATE_INTERVAL); 
    } 


    public class MyLocationList implements LocationListener { 

     public void onLocationChanged(Location arg0) { 
      UpdateWithNewLocation(arg0); 
     } 

     public void onProviderDisabled(String provider) { 
      Toast.makeText(getApplicationContext(), "GPS Disable ", 
        Toast.LENGTH_LONG).show(); 
     } 

     public void onProviderEnabled(String provider) { 
      Toast.makeText(getApplicationContext(), "GPS enabled", 
        Toast.LENGTH_LONG).show(); 
     } 

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

     } 

    } 
}