Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Service to receive SMS when app is in background

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

like image 959
SteffDroid Avatar asked Sep 19 '16 16:09

SteffDroid


People also ask

How do I enable SMS to receive apps on Android?

In some versions of Android, this permission is turned on by default. In other versions, this permission is turned off by default. To set the app's permission on a device or emulator instance, choose Settings > Apps > SMS Messaging > Permissions, and turn on the SMS permission for the app.

How do I run background services on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user click on text view, it will start service and stop service.

How do I know which app sends SMS?

AFAIK you cannot detect which app is sending the SMS. It's just not saved in the content storage. However, you could use an error-prone workaround: list all apps currently running which have the SEND_SMS permission (possibly with internet permission as well) when the SMS is sent.


2 Answers

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

like image 162
SteffDroid Avatar answered Oct 02 '22 22:10

SteffDroid


Setting broadcast priority to 2147483647 may solve the problem as mentioned here.

<receiver android:name=".SmsReceiver">
    <intent-filter android:priority="2147483647" >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>
like image 28
Armin Avatar answered Oct 02 '22 22:10

Armin