Receiving SMS messages
This recipe will demonstrate how to set up a Broadcast Receiver to notify you of new SMS messages. It's useful to note that your app does not need to be running to receive the SMS Intent. Android will start your service to process the SMS.
Getting ready
Create a new project in Android Studio and call it ReceiveSMS
. Use the default Phone & Tablet option and select Empty Activity when prompted for Activity Type.
How to do it...
We won't be using a layout in this demonstration as all the work will be in the Broadcast Receiver. We'll use Toasts to display incoming SMS messages. Open the Android Manifest and follow these steps:
Add the following permission:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
Add the following declaration for the broadcast receiver to the
<application>
element:<receiver android:name=".SMSBroadcastReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED">...