마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

죽어도 다시 살아나는 서비스 구현..

0 추천

일단 그 기능을 하는 레지스터는 인터넷에서 보고 따라만들었습니다..

public class PersistentService extends Service {
private static final String LOG_TAG = "NEVER_DIE";
private static final int REBOOT_DELAY_TIMER = 10 * 1000;
 
@Override
public void onCreate() {
 
Log.d(LOG_TAG, "onCreate");
unregisterRestartAlarm();
 
Intent i = new Intent(this, RestartService.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
 
super.onCreate();
 
}
 
@Override
public void onDestroy() {
 
Log.d(LOG_TAG, "onDestroy");
registerRestartAlarm();
 
super.onDestroy();
}
 
public int onStart(Intent intent, int flags, int startId) {
 
return START_STICKY;
}
 
private void registerRestartAlarm() {
 
Log.d(LOG_TAG, "registerRestartAlarm");
Intent intent = new Intent(PersistentService.this, RestartService.class);
intent.setAction(RestartService.ACTION_RESTART_MAIN_ACTIVITY);
PendingIntent sender = PendingIntent.getBroadcast(PersistentService.this, 0, intent, 0);
 
long firstTime = SystemClock.elapsedRealtime();
firstTime += REBOOT_DELAY_TIMER;
 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, REBOOT_DELAY_TIMER, sender);
}
 
private void unregisterRestartAlarm() {
 
Log.d(LOG_TAG, "unregisterRestartAlarm");
Intent intent = new Intent(PersistentService.this, RestartService.class);
intent.setAction(RestartService.ACTION_RESTART_MAIN_ACTIVITY);
PendingIntent sender = PendingIntent.getBroadcast(PersistentService.this, 0, intent, 0);
 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(sender);
}
 
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
 
 
 
 
==============================================================
public class RestartService extends BroadcastReceiver{
private static final String LOG_TAG = "NEVER_DIE";
public static final String ACTION_RESTART_MAIN_ACTIVITY = "ACTION.Restart.PersistentService";
 
@Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "RestartService called!!!!!!!!!!!!!!!!!!!!!!!");
      
        // 서비스 죽일때 알람으로 다시 서비스 등록
        if(intent.getAction().equals(ACTION_RESTART_MAIN_ACTIVITY)) {
           
            Log.d(LOG_TAG, "Service dead, but resurrection");
           
            Intent i = new Intent(context, MainActivity.class);
            context.startService(i);
        }
       
        // 폰 재부팅할때 서비스 등록
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
           
            Log.d(LOG_TAG, "ACTION_BOOT_COMPLETED");
           
            Intent i = new Intent(context, MainActivity.class);
            context.startService(i);
        }
    }
 
}

 

==========================메니페스트

 

<service
            android:name="com.yun.neverdie.PersistentService"
            android:endable="true"
            android:process=":remote">
           
            <intent-filter>
                <action android:name = "com.yun.nerverdie.MainActivity.class" />
            </intent-filter>
        </service>
 
        <receiver
            android:name="com.yun.neverdie.RestartService"
            android:enabled="true"
            android:exported="false"
            android:label="RestartService"
            android:process=":remote" >
            <intent-filter>
                <action android:name="ACTION.RESTART.PersistentService" />
                <!-- <action android:name="Intent.ACTION_BOOT_COMPLETED "/> -->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
윤둥이 (4,560 포인트) 님이 2013년 7월 16일 질문
매니페스트 서비스쪽에 Exported service does not require permission

이런 에러가 나는데 뭔가요..

제가 서비스에 대한 개념이 잘 없어서 그런데

액티비티에서 서비스를 바인딩 시켜야하나요?

아니면 서비스에서 액티비티를 호출해야하나요?

종료버튼 하나만 있는 앱을 구상중인데

종료버튼으로 죽여도 서비스가 액티비티를 다시 살리도록..

그럼 메인엑티비티에서는 뭘 구현해야 하나요?

startService 하면 되나요..? 정말  좀 도와주세요 ㅠ

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...