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.
The strange things happen here:
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
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.
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.
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.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With