-1

I am developing an Android Application in which I want to check every incoming message on the device in order to detect if any incoming message contains my Keyword. I want this to be done in background (even if application is not running). Please tell how can I do that?

Raees Khan
  • 371
  • 1
  • 5
  • 20
  • Create a service to read incoming message and perform requisite task. – Mohd Asim Suhail Feb 01 '17 at 22:11
  • @MohdAsimSuhail can you please give a simple example in answer. – Raees Khan Feb 01 '17 at 22:12
  • 1
    http://stackoverflow.com/questions/4637821/how-to-analyze-incoming-sms-on-android http://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages http://stackoverflow.com/questions/35587013/how-to-receive-incoming-sms-on-android-kitkat-or-above http://stackoverflow.com/questions/11306150/read-and-display-incoming-message-text-android http://stackoverflow.com/questions/15911670/register-broadcastreceiver-for-incoming-sms http://stackoverflow.com/questions/11435354/receiving-sms-on-android-app http://stackoverflow.com/questions/21995244/ – Mike M. Feb 01 '17 at 22:19

2 Answers2

0

Use a BroadcastReceiver to catch all incoming messages then send the message body to a Service to perform all your required task. Do not perform any long task in your BroadcastReceiver since it runs on the main thread.

Here is an example of how you can catch incoming SMS:

public class SmsListener extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
            for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                String messageBody = smsMessage.getMessageBody();
                // Send the message body to your custom service.
            }
        }
    }
}
DustyMan
  • 366
  • 3
  • 9
0
  1. Make changes in your Manifest to give your app the permissions to receive SMS (WRITE_SMS, READ_SMS, RECEIVE_SMS)

  2. Create a broadcast receiver to process the received SMS intent from service

    private class SMSBroadcastReceiver extends BroadcastReceiver
    {    
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Bundle intentExtras = intent.getExtras();
    
            String message = "";
    
            if ( extras != null )
            {
                Object[] extras = (Object[]) intentExtras.get( "pdus" );
    
                for ( int i = 0; i < extras.length; i++ )
                {
                    SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);
    
                    String message = smsmsg.getMessageBody().toString();
                    //process your message
    
                 }
    
            }
    
        }
    

    }

  3. Create a service class and register it to receive the android.provider.Telephony.SMS_RECEIVED intent filter:

    public class SMSService extends Service { private SMSBroadcastReceiver SMSreceiver; private IntentFilter intentFilter;

        @Override
        public void onCreate()
        {
            super.onCreate();
    
            //SMS event receiver
            SMSreceiver = new SMSBroadcastReceiver();
            intentFilter = new IntentFilter();
            mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
            registerReceiver(SMSreceiver, intentFilter);
        }
    
        @Override
        public void onDestroy()
        {
            super.onDestroy();
            unregisterReceiver(mSMSreceiver);
        }
    }
    
Mohd Asim Suhail
  • 2,176
  • 1
  • 16
  • 23
  • The Google administrators for the Google Play Store consider the RECEIVE_SMS permission (in the tutorial you mention) to be dangerous. As a result, an app that contains the permission will be rejected. Then the developer has to submit a form to Google Play administrators for approval. Other developers have mentioned the process is awful with feedback taking weeks and receiving outright rejections with either no explanations or generic feedback. Any ideas on how to avoid? – AJW Jul 15 '20 at 15:23