일단 그 기능을 하는 레지스터는 인터넷에서 보고 따라만들었습니다..
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>