title: Android Service to receive SMS when app is in background
body: <p>I'm currently working on an app that processes incoming SMS. As I was searching for a solution I came across different ways including stand-alone BroadcastReceivers and Services which work fine. The goal is to receive the SMS even when app is in background and screen is off (phone locked).</p>
  <p>My current problem is: I receive any SMS with a BroadcastReceiver running within a Service. This Service is started in onResume() of my MainActivity and should not be stopped.</p>
  <ul>
  <li>When my App is in foreground, all SMS are recognized by the Receiver.</li>
  <li>Even when app is in background but my phone is still on, I can receive those messages.</li>
  </ul>
  <p>The strange things happen here:</p>
  <ul>
  <li>App is in foreground and I turn the screen off --&gt;SMS are recognized.</li>
  <li>App is in background and I turn the screen off --&gt;SMS wont be recognized.</li>
  </ul>
  <p>This is my Code for Service-class:</p>
  <pre class="lang-java prettyprint-override"><code>public class SmsProcessService extends Service {
      SmsReceiver smsReceiver = new SmsReceiver();
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          registerReceiver(smsReceiver, new IntentFilter(&quot;android.provider.Telephony.SMS_RECEIVED&quot;));
  
          return START_STICKY;
      }
  
      private class SmsReceiver extends BroadcastReceiver {
          @Override
          public void onReceive(Context context, Intent intent) {
              String telnr = &quot;&quot;, nachricht = &quot;&quot;;
  
              Bundle extras = intent.getExtras();
  
              if (extras != null) {
                  Object[] pdus = (Object[]) extras.get(&quot;pdus&quot;);
                  if (pdus != null) {
  
                      for (Object pdu : pdus) {
                          SmsMessage smsMessage = getIncomingMessage(pdu, extras);
                          telnr = smsMessage.getDisplayOriginatingAddress();
                          nachricht += smsMessage.getDisplayMessageBody();
                      }
  
                      // Here the message content is processed within MainAct
                      MainAct.instance().processSMS(telnr.replace(&quot;+49&quot;, &quot;0&quot;).replace(&quot; &quot;, &quot;&quot;), nachricht);
                  }
              }
          }
  
          private SmsMessage getIncomingMessage(Object object, Bundle bundle) {
              SmsMessage smsMessage;
  
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                  String format = bundle.getString(&quot;format&quot;);
                  smsMessage = SmsMessage.createFromPdu((byte[]) object, format);
              } else {
                  smsMessage = SmsMessage.createFromPdu((byte[]) object);
              }
  
              return smsMessage;
          }
      }
  }
  </code></pre>
  <p>Inside my MainActivity I declare an Intent:</p>
  <pre class="lang-java prettyprint-override"><code>Intent smsServiceIntent;
  </code></pre>
  <p>which is initialized in onCreate() like this:</p>
  <pre class="lang-java prettyprint-override"><code>smsServiceIntent = new Intent(MainAct.this, SmsProcessService.class);
  </code></pre>
  <p>I start the Service in Activity onResume():</p>
  <pre class="lang-java prettyprint-override"><code>MainAct.this.startService(smsServiceIntent);
  </code></pre>
  <p>My manifest-file has <code>&lt;service android:name=&quot;.SmsProcessService&quot; /&gt;</code> to declare the Service and following permissions:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;uses-permission android:name=&quot;android.permission.RECEIVE_SMS&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.READ_SMS&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.SEND_SMS&quot; /&gt;
  </code></pre>
  <p>I really hope, you can give me some support and help to figure out the apps's behavior.</p>