John Rambo
John Rambo

Reputation: 934

Android: IntentService getting terminated

I have a IntentService which runs forever as I am using a infinite while loop. However, when I close my application the service gets terminated. Maybe I am wrong, but as far as I know, the IntentService should run in background until it finishes the task.

Moreover, as I am using infinite while loop, the service should run forever. But clearly this not what is happening.

public class TaskNotifierService extends IntentService {

    public static final String TAG="TaskNotifierIS";

    public TaskNotifierService(){
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        while(true){
            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            String currentDay=null;

            switch(day){
                case 1:
                    currentDay="Sunday";
                    Log.i("currentDay","Current day is: "+currentDay);
                    break;
                case 2:
                    currentDay="Monday";
                    Log.i("currentDay","Current day is: "+currentDay);
                    break;
                case 3:
                    currentDay="Tuesday";
                    Log.i("currentDay","Current day is: "+currentDay);
                    break;
                case 4:
                    currentDay="Wednesday";
                    Log.i("currentDay","Current day is: "+currentDay);
                    break;
                case 5:
                    currentDay="Thursday";
                    Log.i("currentDay","Current day is: "+currentDay);
                    break;
                case 6:
                    currentDay="Friday";
                    Log.i("currentDay","Current day is: "+currentDay);
                    break;
                case 7:
                    currentDay="Saturday";
                    Log.i("currentDay","Current day is: "+currentDay);
                    break;
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }


}

Can somebody explain, what is going wrong in here?

Upvotes: 2

Views: 381

Answers (2)

SHICONG CAO
SHICONG CAO

Reputation: 219

IntentService is designed to process an incoming intent and stop immediately after. So in your case I strongly recommend you to replace IntentService by Service.

Both Service and IntentService will be killed while the application is killed, you can register a WatchAlarm in service onCreate function to start self in a controlled period. You can check the code below:

@Override
public void onCreate() {
    registerWatchAlarm();
}

private void registerWatchAlarm() {
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Intent startSelf = new Intent(this, BoostBallService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, startSelf,
            PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + WAKE_UP_TIME_INTERVAL, WAKE_UP_TIME_INTERVAL, pi);
}

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317948

Android can kill any app process at any time without warning. It has a priority system by which it decides which processes get killed first. Having a service active doesn't prevent your process from getting killed, especially if it has no visible UI.

You can read more details about the priority system here. Start at the following text:

The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes.

Upvotes: 0

Related Questions