Search code examples
androidbroadcastreceiversleepdelaybootcompleted

BroadcastReceiver for BOOT_COMPLETED not returning from sleep()


I have a BroadcastReceiver that reacts on BOOT_COMPLETED. In turn it shall issue an intend, but this intend should be delayed for a while. For some reason, if the parameter to the sleep() call is greater than approx. 6000ms, the sleep never returns (I never see the "Sleep done!" message in the code below). There is also no exception thrown. Below 6000ms everything works fine!

Any Idea how to solve this would be great!

public class BootReceiver extends BroadcastReceiver
{   
    @Override
    public void onReceive( final Context context, final Intent intent)
    {                  
        Thread thr = new Thread ()
        {
            @Override
            public void run()
            {
                Log.e ( "Test", "Going to sleep!" );

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

                Log.e ("Test","Sleep done!");
            }
        };
        thr.start();
    }
}

Solution

  • I have a BroadcastReceiver that reacts on BOOT_COMPLETED. In turn it shall issue an intend, but this intend should be delayed for a while.

    Then you should use AlarmManager to set() a PendingIntent to go off after your specified delay.

    For some reason, if the parameter to the sleep() call is greater than approx. 6000ms, the sleep never returns (I never see the "Sleep done!" message in the code below).

    You have the BroadcastReceiver equivalent of an "Application Not Responding" (ANR) condition, because you tied up the main application thread for too long.

    There is also no exception thrown.

    Yes, but there is a warning in LogCat -- at least, there used to be.