2014-04-10 5 views
1

Android 개발을 처음 사용하면서 앱이 백그라운드 모드에서 40 초마다 GPS 위치를 얻길 원합니다. 살해 당했다. 이를 위해 MyAlarm.class에서 40 초마다 Alarm을 setRepeating하여 보류중인 인 텐트를 사용하여 "RepeatingAlarm.class (BroadcastReceiver를 확장 함)"를 호출합니다. 40 초마다 호출되는 "RepeatingAlarm.class"의 onReceive 메소드에서, MyReceiver.class (BroadcastReceiver를 확장)를 호출하는 또 다른 보류 인 텐트를 만들었습니다. "RepeatingAlarm.class"에서 생성 된 보류중인 인 텐트를 requestLocationUpdate 함수에 전달하여 GPS 위치를 가져 왔습니다.위치 관리자 .RequestLocationUpdent가 동일한 위치를 항상 브로드 캐스팅 할 때 gps 제공 업체가 위치를 얻는 데 오랜 시간이 걸릴 때

제 문제는 때로는 약 3 분 동안 계속해서 40 초마다 똑같은 위도와 경도 값을 반복적으로 얻을 것입니다.

그런 다음 GPS 수신 위치를 수신 한 후 호출하는 대신 MyReceiver.class의 onReceive 메소드가 매초마다 호출합니다. 아래 코드를 붙여 넣었습니다. 해결책을 찾도록 도와주세요.

MyAlarm.class 

public void StartAlarm(Context context) 
{ 
AlarmManager alm=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(context, RepeatingAlarm.class); 
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); 
alm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 40000, pi); 
} 

RepeatingAlarm.class 

public class RepeatingAlarm extends BroadcastReceiver 
{ 
public static LocationManager locationManager = null; 
public static PendingIntent pendingIntent = null; 
@Override 
public void onReceive(Context context, Intent intent) 
{ 
    locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); 
    Intent intentp = new Intent(context, MyReceiver.class); 
    pendingIntent = PendingIntent.getBroadcast(context, 0, intentp, PendingIntent.FLAG_UPDATE_CURRENT); 
    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    provider = locationManager.getBestProvider(criteria, true); 
    if(provider != null) 
    {    
    locationManager.requestSingleUpdate(locationManager.GPS_PROVIDER, pendingIntent); 
    } 
} 
} 

MyReceiver.class 

public class MyReceiver extends BroadcastReceiver 
{ 
@Override 
public void onReceive(Context context, Intent intent) 
{ 
    String locationKey = LocationManager.KEY_LOCATION_CHANGED; 
    if (intent.hasExtra(locationKey)) 
    { 

    Location location = (Location)intent.getExtras().get(locationKey); 
    double mlatitude = location.getLatitude(); 
    double mlongitude = location.getLongitude(); 
    if(RepeatingAlarm.locationManager != null && RepeatingAlarm.pendingIntent) 
    { 
    RepeatingAlarm.locationManager.removeUpdates(RepeatingAlarm.pendingIntent); 
    } 

    } 
} 
} 

위의 코드에서 GPS 위치는 매 40 초마다 한 번 수신됩니다. 그러나 GPS가 한 번 위치 정보를 얻기 위해 오랜 시간이 걸리면 15 분, 그 후 매 40 초마다 같은 이전 위치가 약 4 분까지 반복됩니다. 이것이 나의 주요한 문제이다.

그런 다음 MyReceiver.class가 매 초마다 자주 호출합니다. 이 문제를 해결하기 위해 몇 가지 예제 코드 라인을 도와주십시오. 다들 감사 해요.

+0

GPS가 처음 수정 될 때를 알고 싶다면이 질문에 대한 내 대답의 코드를 사용할 수 있습니다. http://stackoverflow.com/questions/19365035/location-servise-gps-force-closed/ 19366773 # 19366773 – cYrixmorten

+0

@cYrixmorten : GPS를 듣는 데 오랜 시간이 걸리는 지점 (버스 나 기차 또는 공공 장소가 더 혼잡 한 곳)에서 위치가 중복되는 (즉, 동일한 위치를 제공하는) 이유를 말해 주실 수 있습니까?). 내 코드에서 실수라고 생각하지만 예측할 수 없었습니다. 감사. – Raj

+0

솔직히 말하면, 나는 그 이유를 지금 말할 수 없다. 내가 그 물건으로 뛰어 들었으니 꽤 오랜 시간이 걸렸지 만 Criteria.ACCURACY_FINE 기준에도 불구하고 GPS가 첫 번째 문제를 발견하지 못하면 lastKnownLocation을 반환합니다. 이러한 신뢰할 수없는 순위의 결과는 정확한 위치를 제공 할 수있는시기를 정확하게 알려주는 코드를 작성해야하는 어려움을 겪었습니다. – cYrixmorten

답변

0

개발자 문서 requestSingleUpdate() "이름이 지정된 공급자 및 보류중인 인 텐트를 사용하여 단일 위치 업데이트 등록"방법에 따라.

대신 requestLocationUpdates() 메서드를 사용해야합니다.

이 메서드의 두 번째 매개 변수 minimum time interval between location updates, in milliseconds을 사용하면 다시 & 위치를 가져올 수 있습니다.

+0

나는 이미 requestLocationUpdates() 함수도 시도했다.여전히 중복 위치가 존재합니다. 보류중인 방송을 방송하는 방식이 제 코드에 맞다고 말할 수 있습니까? StartAlarm.class와 RepeatingAlarm.class 모두에 있습니다. 감사. – Raj

+0

@Raj :'requestLocationUpdates()'를 사용한다면, AlarmManager 메소드가 필요 없다. 그것은 둘 중 하나가 아닌 둘 중 하나입니다. 그래서 중복 된 위치를 얻는 것입니다. – ChuongPham

+0

AlarmManager가 매 40 초마다 호출하여 requestSingleUpdates() 함수를 사용하려고합니다. 그리고 나서, MyReceiver.class의 onRecieve 메소드는 requestingSingleUpdates()가 GPS를 얻는 순간 호출되는 순간부터 매 초마다 호출합니다. – Raj