일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 안드로이드
- 뮤직비디오
- 레인보우
- 자바
- 바닐라루시
- 인텐트
- TOEIC
- 서비스
- 네트워크보안
- 쓰레드
- Eclipse
- 영어
- error
- 엑티비티
- Intent
- 토익
- opengl
- Linux
- 배다해
- xml
- HTML
- 브로드케스트 리시버
- Looper
- opencv
- Android
- Java
- 네트워크
- 이클립스
- 걸스데이
- rainbow
- Today
- Total
W.A 저장소
Immortal Service(죽지않는 서비스) 본문
현재 진행하는 프로젝트를 위해 죽어도 다시 바로 살아날 수 있는 서비스가 필요하여 간단한 서비스가 죽게 되면은 AlarmManager를 이용하여 다시 살아날 수 있는 서비스를 만들어보았다.
시작은 우선 기본적인 서비스 시작과 중지를 담당하는 MainActivity가 있다.(ImmortalServiceMainActivity)
주가되는 것은 PersistentService.java & RestartService.java 두 가지가 있다. 이 두개를 이용하여 서비스가 중지가 되면 AlarmManager를 이용하여 서비스를 다시 실행하게 만들어준다. 위의 것을 사용하기 위해서는 Service의 생명주기를 알아야 한다.
그러면 이제 부터는 소스를 살펴보자.
PersistentService.java
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.d(TAG, "Service Create");
unregisterRestartAlarm(); //등록된 알람은 제거
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.d(TAG, "Service Destroy");
registerRestartAlarm(); // 서비스가 죽을 때 알람 등록
super.onDestroy();
}
void registerRestartAlarm() {
Log.d(TAG, "registerRestartAlarm");
Intent intent = new Intent(PersistentService.this, RestartService.class);
intent.setAction(RestartService.ACTION_RESTART_PERSISTENTSERVICE);
PendingIntent sender = PendingIntent.getBroadcast(
PersistentService.this, 0, intent, 0); // 브로드케스트할 Intent
long firstTime = SystemClock.elapsedRealtime(); // 현재 시간
firstTime += 1 * 1000; // 10초 후에 알람이벤트 발생
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); // 알람 서비스 등록
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
10 * 1000, sender); // 알람이
}
RTC_WAKEUP : 알람 설정 시 지정된 시계 시간에 Intent를 생성하도록 장치를 깨움
RTC : 명시적인 시간에 Intent를 발생시킬 것이지만, 장치를 깨우지는 않음
ELAPSED_REALTIME : 장치가 부팅되고 난 이후로 경과된 시간의 양에 기반해 Intent 발생. 장치를 깨우지는 않음. 절전상태도 포함
ELAPSED_REALTIME_WAKEUP : 장치가 부팅된 이후로 지정된 길이의 시간이 지나면 intent발생 후 필요한 경우 장치를 깨움
void unregisterRestartAlarm() {
Log.d(TAG, "unregisterRestartAlarm");
Intent intent = new Intent(PersistentService.this, RestartService.class);
intent.setAction(RestartService.ACTION_RESTART_PERSISTENTSERVICE);
PendingIntent sender = PendingIntent.getBroadcast(
PersistentService.this, 0, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
}
RestartService.java
@Override
public void onReceive(Context context, Intent intent) {
//TODO Auto-generated method stub
Log.d("ImmortalService", "RestartService called!@!@@@@@#$@$@#$@#$@#");
if(intent.getAction().equals(ACTION_RESTART_PERSISTENTSERVICE)) {
Intent i = new Intent(context, PersistentService.class);
//Intent i = new Intent(this, PersistentService.class);
context.startService(i);
}
}
AndroidManifest.xml
<receiver android:name="RestartService" android:process=":remote">
<intent-filter>
<action android:name="ACTION.Restart.PersistentService"></action>
</intent-filter>
</receiver>
위와 같이 다 코딩을 완료하였으면 최초에 서비스를 실행하고 서비스를 죽일 경우
1. AlarmManager 등록
2. 정해진 시간이 흐른 뒤 Intent를 BroadCasting
3. BroadcastReceiver는 Broadcast된 Intent를 받고 미리 정의한 행동(PersistentService 살리기)을 수행
'Programing' 카테고리의 다른 글
Get Process Info( CPU Usage / Memory Usgae) (0) | 2010.11.04 |
---|---|
Get PackageName(패키지 이름 받아오기) (0) | 2010.10.28 |
OpenCV for android 사용기 #1 (0) | 2010.09.15 |
Telephony, Notification & Alarm (0) | 2010.08.24 |
Broadcast Receiver (0) | 2010.08.24 |