SMS Parser - Android
Receiving and Parsing SMS Messages on Android Devices
Introduction
This module was created for getting specific codes out of incoming SMS messages. To use it you create a Config object where you specify the BEGIN and END message phrases & the SMS sender numbers from which you want to read the sms content. The received code then can be used according to your need (activation, authentication etc...).
How it Works
If you get a SMS message like this:
This is an automated message used by X-App. You can delete this message at any time. BEGIN-MESSAGE 08ff08d3b2981eb6c611a385ffa4f865 END-MESSAGE
and the sender of the message is one of the numbers you have specified in your Config object the parsing process will begin.
First the module reads the whole message. It then checks if it contains the BEGIN and END phrases you specified. If it does then it takes only the part that is between those phrases and send a broadcast to you app with the specific code and the sender phone number. In this case the module would send a broadcast containing 08ff08d3b2981eb6c611a385ffa4f865
and the sender number.
For more info read Usage.
Usage
Add the module to your apps build.gradle:
compile 'de.adorsys.android:smsparser:0.0.3'
First of all you have to create a Config object to configure the modules use:
SmsConfig.INSTANCE.initializeSmsConfig(
"BEGIN-MESSAGE",
"END-MESSAGE",
"0900123456", "0900654321", "0900900900");
Here the two parameters are the keywords that will be used for the code to be extracted from the sms message. The third parameter is a varargs (...) parameter, where you can give a series of phone numbers (as Strings), which will be checked against for reading sms content.
Then before startinganything you have to aks for the SMS Permmision on Android 6.0 and higher, or else the module won't work:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
SmsTool.requestSMSPermission(Context);
}
Then you need to create a LocalBroadcastManager and BroadcastReceiver, like this:
@NonNull
private LocalBroadcastManager localBroadcastManager;
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SmsReceiver.INTENT_ACTION_SMS)) {
String receivedSender = intent.getStringExtra(SmsReceiver.KEY_SMS_SENDER);
String receivedMessage = intent.getStringExtra(SmsReceiver.KEY_SMS_MESSAGE);
smsSenderTextView.setText(getString(R.string.text_sms_sender_number,
receivedSender != null ? receivedSender : "NO NUMBER"));
smsMessageTextView.setText(getString(R.string.text_sms_message,
receivedMessage != null ? receivedMessage : "NO MESSAGE"));
}
}
}
;
LocalBroadcastManager is used instead of standard BroadcastManager for security reasons, so no other app can listen to the broadcasts that are sent.
You must create register and unregister methods for the BroadcastReceiver and call them in onResume() and onPause(), respectively:
@Override
protected void onResume() {
registerReceiver();
super.onResume();
}
@Override
protected void onPause() {
unRegisterReceiver();
super.onPause();
}
private void registerReceiver() {
localBroadcastManager = LocalBroadcastManager.getInstance(Context);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SmsReceiver.INTENT_ACTION_SMS);
localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter);
}
private void unRegisterReceiver() {
localBroadcastManager.unregisterReceiver(broadcastReceiver);
}