× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Non freezing sleep in android app

Xaver Kapeller
Mar 13, 2015
<p><strong>NEVER</strong> use <code>Thread.sleep()</code>. If you do, you are blocking the current <code>Thread</code>. That means if you were to call <code>Thread.sleep()</code> on the UI <code>Thread</code> the UI would freeze and you couldn't interact with it anymore, just like you experienced. There are multiple options to schedule some task or event for the future. You can:</p> <ol> <li>Use a <code>Timer</code></li> <li>Use a <code>Handler</code> with <code>postDelayed()</code></li> <li>Use the <code>AlarmManager</code></li> </ol> <hr> <h1>Using a <code>Timer</code></h1> <p><code>Timer</code> can be used to schedule a <code>TimerTask</code>. They are best suited to schedule tasks a few seconds to maybe a few minutes into the future.</p> <p>First you have to write a <code>TimerTask</code>. This task will be executed once the time has elapsed. A <code>TimerTask</code> might look like this:</p> <pre><code>private class ExampleTask extends TimerTask { @Override public void run() { // This method is called once the time is elapsed } } </code></pre> <p>And you can schedule a <code>TimerTask</code> like this:</p> <pre><code>Timer timer = new Timer(); ExampleTask task = new ExampleTask(); // Executes the task in 500 milliseconds timer.schedule(task, 500); </code></pre> <hr> <h1>Using a <code>Handler</code> with <code>postDelayed()</code></h1> <p>Using a <code>Handler</code> is pretty similar to using a <code>Timer</code>, but personally I prefer using a <code>Timer</code> as they are just better suited for jobs like this. <code>Handler</code> were originally meant to facilitate communication between <code>Threads</code> and other things, but they can also be used to scheduling a <code>Runnable</code>. First we have to define a <code>Runnable</code>, this is pretty similar to the <code>TimerTask</code>:</p> <pre><code>Runnable runnable = new Runnable() { @Override public void run() { // This method will be executed in the future } }; </code></pre> <p>And now we schedule the <code>Runnable</code> to be executed in the future with <code>postDelayed()</code>:</p> <pre><code>Handler handler = new Handler(); // Execute the Runnable in 500 milliseconds handler.postDelayed(runnable, 500); </code></pre> <hr> <h1>Using the <code>AlarmManager</code></h1> <p>The <code>AlarmManager</code> is a system service and can be used to send an <code>Intent</code> at some point in the future. You can schedule this <code>Intent</code> for months or even years into the future with the <code>AlarmManager</code>, the only drawback is that you have to reset all alarms when the phone is rebooted.</p> <p>You can get the <code>AlarmManager</code> like this:</p> <pre><code>AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); </code></pre> <p>You also need to wrap the <code>Intent</code> you want to send in a <code>PendingIntent</code>:</p> <pre><code>Intent intent = new Intent(SOME_ACTION); ... // Setup your intent PendingIntent pendingIntent = PendingIntent.getService(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT); </code></pre> <p>And you can schedule the <code>Intent</code> to be sent at a specific <code>Date</code> like this:</p> <pre><code>manager.set(AlarmManager.RTC, date.getTime(), pendingIntent); </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/24575954/Non%20freezing%20sleep%20in%20android%20app/24576043">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus