Code Monkey home page Code Monkey logo

sms-parser-android's Introduction

SMS Parser - Android

Receiving and Parsing SMS Messages on Android Devices

Build Status Download Android Arsenal API License Open Source Love

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);
    }

Contributors:

@drilonreqica

@itsmortoncornelius

sms-parser-android's People

Contributors

drilonrecica avatar isabellacaspari avatar emjeschke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.