2011-11-22 2 views
0

Android를 처음 사용하며 다음과 같은 문제가 있습니다. 필자는 모든 위치 공급자를 먼저 확인하여 마지막으로 알려진 위치를 얻는 인 텐트 서비스가있는 샘플 응용 프로그램을 작성합니다. 마지막으로 알려진 위치 중 어느 것도 정확한 (또는시기 적절한) 위치를 제공하지 않으면 위치 관리자의 requestLocationUpdates 메소드가 BroadcastReceiver 인 텐트와 함께 호출됩니다. 브로드 캐스트 리시버의 onReceive 메서드가 호출 될 때마다 위치가 정확하고시기 적절한 지 확인해야합니다. 또한 Intent 서비스에서 TimerTask를 사용하여 결국에는 결과가 정확하고시기 적절한 위치 업데이트가 있는지 확인해야합니다. 내가 가지고있는 문제는 브로드 캐스트 수신기에서 얻은 위치 데이터를 인 텐트 서비스로 다시 가져 오는 방법을 모른다는 것입니다. 이것이 쉬운 일이되어야하는 것처럼 보입니다. 그러나 나는 이것을 며칠 동안 고민해 왔습니다. 내가 할 수있는 유일한 방법은 데이터를 SQLite DB에 브로드 캐스트 리시버에 쓰고 그 레코드를 인 텐트 서비스에서 다시 읽는 것이다.하지만 이것은 불필요하게 복잡해 보인다. 누구든지 데이터를 의도 서비스로 다시 가져 오는 것이 옳은 방법을 알고 있습니까? requestLocationUpdates에 브로드 캐스트 리시버를 사용해야합니까? 이 작업을 수행하는 더 쉬운 방법이 있습니까? 도움을android development : requestLocationUpdates를 통해 호출 된 BroadcastReceiver에서 데이터를 가져옵니다.

public class HandleLocationUpdateReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
    Location loc = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED); 
    if (loc != null) 
    { 
     double lat = loc.getLatitude(); 
     double lon = loc.getLongitude(); 
     // Do some checking to see how accurate and timely the location is 
     // here and somehow get it back to the intent service. 
    } 
    } 
} 

감사합니다 다음은 코드

다음
public class GetLocationService extends IntentService { 

    public GetLocationService() { 
     super("something"); 
    } 

    LocationManager locationManager; 
    long maxFixLateness; 
    float maxFixPosUncertainty; 
    boolean usableLocObtained; 
    Location bestLoc = null; 
    float bestLocScore = 0; 
    Intent locChangeI; 
    PendingIntent pLocChangeI; 

    @Override 
    final protected void onHandleIntent(Intent intent) { 
     maxFixLateness = 30000; 
     maxFixPosUncertainty = 30; 
     long curTime = System.currentTimeMillis(); 
     LocationManager locationManager = (LocationManager) this 
       .getSystemService(Context.LOCATION_SERVICE); 
     // Check for a usable location fix 
     List<string> matchingProviders = locationManager.getAllProviders(); 
     for (String provider : matchingProviders) { 
      Location location = locationManager.getLastKnownLocation(provider); 
      if (location != null) { 
       // ...some code to check if the location is accurate or timely 
       // enough 
      } 
     } 
     if (bestLoc == null) { 
      locChangeI = new Intent(this, HandleLocationUpdateReceiver.class); 
      pLocChangeI = PendingIntent.getBroadcast(this, 0, locChangeI, 
        PendingIntent.FLAG_UPDATE_CURRENT); 
      usableLocObtained = false; 
      locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 0, 0, pLocChangeI); 
      locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 0, 0, pLocChangeI); 
      // Call the timer that will periodically check to see if a usable 
      // location has been obtained. 
      new LocFixCheckTimer(60000, 30, 1000); 
     } 
    } 

    private class LocFixCheckTimer { 

     Timer timer; 
     long numChecks; 

     public LocFixCheckTimer(long initSearchTime, long maxRechecks, 
       long recheckFreq) { 
      numChecks = maxRechecks; 
      timer = new Timer(); 
      // Wait 2 seconds before checking for a fix again 
      timer.schedule(new CheckLocTask(), initSearchTime, recheckFreq); 
     } 

     class CheckLocTask extends TimerTask { 

      public void run() { 
       if (numChecks > 0) { 
        if (usableLocObtained == true) { 
         // I want to use the location data obtained from the 
         // HandleLocationUpdateReceiver's onReceive method 
         // but I don't how to get that data here. 
        } 
       } else { 
        // Cancel the timer. We've timed-out on searching 
        // for a usable location fix 
        timer.cancel(); 
       } 
       --numChecks; 
      } 
     } 
    } 
} 

는 방송 수신기입니다!

답변

0

활동이나 서비스로 데이터를 보내려면 수신기를 사용하십시오. 그것은에서 제공됩니다 this link

+0

나는 지금 일하고있어! 도와 주셔서 감사합니다! – user1057118

관련 문제