Search code examples
androidandroid-broadcast

How to guarantee that my app will always receive every incoming SMS without fail?


I have an app which needs to read every incoming SMS messages. I wrote that by writing a BroadcastReceiver and registered to listen for the intent - android.provider.Telephony.SMS_RECEIVED. Later I realised that it can only receive the broadcasts while the process is in foreground.

As per suggestion in BroadcastReceiver's Javadoc I looked at Services. This looked promising but even this could get killed in case of low memory. I have also seen apps which allow the users to kill background processes, so they too could kill it.

So, how does the bundled SMS client manages to always receive and store the SMS messages?


Solution

  • You need to register your receiver in Android Manifest file. After user launches your app at least once, it will become active. Your receiver will always be notified then. Even if your app is not running and there is an SMS, Android will start your app and send broadcast to it.

    Here you can read more about BroadcastReceiver lifecycle.