title: To catch the moment of receiving SMS
body: <p>I'm creating an application that needs to read incoming SMS. Saving to a SMS provider is not required - just showing the toast at the time of receiving SMS.</p>
  
  <pre><code>&lt;receiver android:name=".SmsHandler"
          android:permission="android.permission.BROADCAST_SMS"&gt;
          &lt;intent-filter android:priority="2147483647"&gt;
              &lt;action android:name="android.provider.Telephony.SMS_RECEIVED"/&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  </code></pre>
  
  <p>Receiver</p>
  
  <pre><code>public class SmsHandler extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Receive sms", Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>
  
  <p>Receiver working when app running, if app kill, receiver not call.</p>
  
  <p>I know that with Android KitKat you need to specify the default applications for working with sms. But this is necessary if you want to save SMS to the SMS provider. This is not required if you need to catch the moment of receiving SMS.</p>
  
  <p>What should I do to make the receiver work when the application is killed?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried to use the service to register BroadcastReceiver</p>
  
  <pre><code>public class AlarmService extends Service {
  private static BroadcastReceiver SmsHandler;
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public void onCreate()
  {
      registerScreenOffReceiver();
  }
  
  @Override
  public void onDestroy()
  {
      unregisterReceiver(SmsHandler);
      SmsHandler = null;
  }
  
  private void registerScreenOffReceiver()
  {
      SmsHandler = new BroadcastReceiver()
      {
          @Override
          public void onReceive(Context context, Intent intent) {
              Toast.makeText(context, "Receive sms", Toast.LENGTH_LONG).show();
          }
      };
      IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
      registerReceiver(SmsHandler, filter);
  }
  </code></pre>
  
  <p>}</p>