crazy_coder
crazy_coder

Reputation: 1049

Thread is being killed by Android system

In my android application, I am performing a task in the receiver that listens to BOOT_UP_Completed broadcast. Because the task duration is indefinite and depends on current system time, I performed the task in a different thread. The thread is started in onReceive() method of BOOT_COMPLETED intent. In the thread task, I sleep the thread for 1 second if the check fails to avoid over-utilization of CPU cycles. The run method looks something like below:

while (true) {

        if (condition == true) {
            //doSomething
            break;
        }

        Thread.sleep(sleep_duration);   // 1 second
    }

When I boot up the device, the thread runs for a while and after that I get the message like below:

I/ActivityManager( 3166): Killing 6916:process/1101 (adj 15): bgCount ##41

I am not sure why the system is killing the thread. Or why the thread stopped executing. Any leads would be appreciated.

Upvotes: 0

Views: 844

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93599

You can't own a long term thread (more than a few seconds) in a BroadcastReceiver. Once a BroadcastReceiver ends, Android may kill the app. To fix this, launch a Service from the BroadcastReceiver and have the Service own the Thread. A service can last as long as needed.

Upvotes: 3

Related Questions