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

by digipine posted Oct 29, 2017
?

Shortcut

PrevPrev Article

NextNext Article

ESCClose

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 •

Articles

1 2