나는 작업을 얻었다. 먼저
안드로이드 예약 작업
6:00 register locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, locationListener);<br>
19:00 unregister locationManager.removeUpdates(locationListener);
, 나는 AlarmManager
으로, 서비스를 사용하고 싶습니다. 하지만 서비스 활동에서 locationManager
및 locationListener
를 보낼 수 없습니다.
마지막으로 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();
}
}
사람은 사전에 잘 계획 방법, 들으있다.