Android Service to receive SMS when app is in background

androidbackgroundbroadcastreceiverservicesms

I'm new to Stackoverflow and currently working on an app that processes incoming SMS. As I was searching for a solution I came across different ways including stand-alone BroadcastReceivers and Services which work fine. The goal is to receive the SMS even when app is in background and screen is off (phone locked).

My current problem is: I receive any SMS with a BroadcastReceiver running within a Service. This Service is started in onResume() of my MainActivity and should not be stopped.

  • When my App is in foreground, all SMS are recognized by the Receiver.
  • Even when app is in background but my phone is still on, I can receive those messages.

The strange things happen here:

  • App is in foreground and I turn the screen off –>SMS are recognized.
  • App is in background and I turn the screen off –>SMS wont be recognized.

This is my Code for Service-class:

public class SmsProcessService extends Service {
    SmsReceiver smsReceiver = new SmsReceiver();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

        return START_STICKY;
    }

    private class SmsReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String telnr = "", nachricht = "";

            Bundle extras = intent.getExtras();

            if (extras != null) {
                Object[] pdus = (Object[]) extras.get("pdus");
                if (pdus != null) {

                    for (Object pdu : pdus) {
                        SmsMessage smsMessage = getIncomingMessage(pdu, extras);
                        telnr = smsMessage.getDisplayOriginatingAddress();
                        nachricht += smsMessage.getDisplayMessageBody();
                    }

// Here the message content is processed within MainAct
                    MainAct.instance().processSMS(telnr.replace("+49", "0").replace(" ", ""), nachricht);
                }
            }
        }

        private SmsMessage getIncomingMessage(Object object, Bundle bundle) {
            SmsMessage smsMessage;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                String format = bundle.getString("format");
                smsMessage = SmsMessage.createFromPdu((byte[]) object, format);
            } else {
                smsMessage = SmsMessage.createFromPdu((byte[]) object);
            }

            return smsMessage;
        }
    }
}

Inside my MainActivity I declare an Intent

Intent smsServiceIntent

which is initialized in onCreate() like this

smsServiceIntent = new Intent(MainAct.this, SmsProcessService.class);

I start the Service in Activity onResume()

MainAct.this.startService(smsServiceIntent);

My manifest-file has <service android:name=".SmsProcessService" /> to declare the Service and following permissions:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

I really hope, you can give me some support and help to figure out the apps's behavior.

Greets Steffen

Best Answer

I tried the app on different phones now and I think I have found the source of the problem.

On the other phone (same OS-Version) everything works just fine. I guess that several other apps also receive the SMS-Broadcast and maybe they disrupt each other. I didn't manage to figure out exactly which apps are the worst.

I will now try to localize other applications with similar use and hope that uninstallation or changing their notification settings will help. Have you any guess how those other apps (like "Auto Ring", which I want to use furthermore) could work parallel?

EDIT:

I found the problem causing app: It's the Google-App which also has SMS-permission granted. I deactivated it and everthing works just fine

Greets Steffen