logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

Android - AlarmManager를 이용해서 서비스 실행 시키는 코드

by digipine posted Oct 29, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

final Intent intent = new Intent(context, MyService.class);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);

Calendar cal = nextScheduleTime();

// AlarmManager을 이용해서, 특정시간에 MyService 서비스가 시작되도록 하는 코드
AlarmManager am = (AlarmManager)context.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5 * 60 * 1000, pending);

 

------------------------------------------------------------------------------------------

 

10초 뒤에, updateIntent가 실행되서, UpdateService가 호출된다.

long nextUpdate = 10*1000;
Log.d(TAG, "Requesting next update in " + nextUpdate + " msec." );

Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
updateIntent.setClass(this, UpdateService.class);

PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);

// Schedule alarm, and force the device awake for this update
AlarmManager alarmManager = (AlarmManager)getBaseContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+nextUpdate,
    nextUpdate, pendingIntent);

TAG •

List of Articles
No. Subject Author Date Views
1 [Java] SortedSet과 Comparable을 이용한 정렬(Sort) digipine 2017.10.29 776
Board Pagination Prev 1 Next
/ 1