2012-04-25 4 views
2

나는 작업을 얻었다. 먼저
안드로이드 예약 작업

6:00 register locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       0, 0, locationListener);<br> 
19:00 unregister locationManager.removeUpdates(locationListener); 

, 나는 AlarmManager으로, 서비스를 사용하고 싶습니다. 하지만 서비스 활동에서 locationManagerlocationListener를 보낼 수 없습니다.

마지막으로 AsyncTask을 사용하여 항상 실행하려면 doInbackground에 루프를 만듭니다. 시간이 6:00 또는 19시 경우, 등록 또는을 locationManager의 등록을 취소합니다.

protected Object doInBackground(Object... params) { 
    Calendar calendar = Calendar.getInstance(); 
    while(flag) { 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.HOUR_OF_DAY, 19); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.SECOND, 0); 

     long now = System.currentTimeMillis(); 
     if(calendar.getTime().getTime() - now < 1000) { 
      publishProgress(new Message(Message.REMOVE_LOCATION_MANAGER)); 
     } 

     calendar.set(Calendar.HOUR_OF_DAY, 6); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.SECOND, 0); 
     now = System.currentTimeMillis(); 
     if(calendar.getTime().getTime() - now < 1000) { 
      publishProgress(new Message(Message.REGISTER_LOCATION_MANAGER)); 
     } 
    } 
    return null; 
} 
protected void onProgressUpdate(Object... values) { 
    super.onProgressUpdate(values); 

    Message msg = (Message)values[0]; 
    if(msg.type == Message.REMOVE_LOCATION_MANAGER) { 
     lm.removeUpdates(ll); 
     Toast.makeText(context, "remove locationManager", Toast.LENGTH_SHORT).show(); 
    } 

    if(msg.type == Message.REGISTER_LOCATION_MANAGER) { 
     lm.removeUpdates(ll); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       0, 0, ll); 
     Toast.makeText(context, "register locationManager", Toast.LENGTH_SHORT).show(); 
    } 
} 

사람은 사전에 잘 계획 방법, 들으있다.

답변

-1

직접 질문에 대답 아무 소용 없다, 당신은 확실히 19PM 오전 6시에서 GPS를 실행하는 동안 어떤 안드로이드 장치가 하나의 배터리 충전에 살아남을 것이기 때문에, 응용 프로그램을 다시 생각해야합니다.

은 기본적으로 당신은 당신이 정말 필요할 때만 GPS 위치 업데이트를 요청해야합니다. http://android-developers.blogspot.jp/2011/06/deep-dive-into-location.html

: 여기에 점점 위치 업데이트에 대한 안드로이드 전문가 레토 마이어에 의해 좋은 기사입니다