title: How can a closed application receive periodic broadcasts?
body: <p>I have a device with Android 9 and I am developing an application that displays content depending on the current date and time. I would like it to regularly check whether new content is available and inform the user about it. Additionally I would like that to work also when the user closes the application. My email application here for example shows notifications about new emails also when it's closed.</p>
  <p>My approach has been to receive general broadcasts (<code>Intent.ACTION_USER_PRESENT</code> and <code>Intent.ACTION_SCREEN_ON</code>), run a timer on receiving and then check regularly for new content. I know that the user has to start my application once for that (<a href="https://stackoverflow.com/questions/9952562/broadcast-receiver-not-working-in-ics-if-the-app-is-not-started-atleast-once">Broadcast receiver not working in ICS if the app is not started atleast once</a>). The approach is <strong>not</strong> successful, if the user uses the multitasking button and swipes my activity up to close it.</p>
  <p>How can I receive broadcasts even though the user has closed my application/activity.</p>
  <p>There are <strong>many</strong> posts here, that ask the same, but most of them seem to be outdated! I have tried so many of them, but I have not managed to get it to work! Therefore I have set up an example application for API 28 and I would like to ask the question again, but this time with a defined example. Then it should be easy to check whether the proposed solution <strong>really</strong> works!</p>
  <ol>
  <li>Create an empty activity in <em>AndroidStudio</em> with <code>File</code>-&gt;<code>New</code>-&gt;<code>New Project...</code>-&gt;<code>Empty Activity</code>-&gt;<code>Language: Java, SDK: API 28</code>.</li>
  <li>Add a class <code>MyBroadcastReceiver</code> with the following content:</li>
  </ol>
  <pre><code>package org.test.myapplication;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.widget.Toast;
  
  public class MyBroadcastReceiver extends BroadcastReceiver
  {
      @Override
      public void onReceive(Context context, Intent intent)
      {
          Toast.makeText(context, &quot;Hurray! I have received the broadcast!&quot;, Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>
  <ol start="3">
  <li>Modify the class <code>MainActivity</code> to have the following content:</li>
  </ol>
  <pre><code>package org.test.myapplication;
  
  import android.content.BroadcastReceiver;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.os.Bundle;
  import android.util.Log;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  public class MainActivity extends AppCompatActivity
  {
      private static BroadcastReceiver broadcastReceiver = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if (null == broadcastReceiver)
          {
              // Register broadcast receiver (only a running application my do that)
              IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
              filter.addAction(Intent.ACTION_SCREEN_ON);
  
              Log.i(&quot;MainActivity&quot;, &quot;Registered broadcast receiver.&quot;);
  
              broadcastReceiver = new MyBroadcastReceiver();
  
              registerReceiver(broadcastReceiver, filter);
          }
      }
  }
  </code></pre>
  <p>When installing and running the activity, I see the toast whenever I log off and on again. But as soon as I have swiped the activity up to close it, I don't see the toast anymore. How can I change that?</p>