I'm developing a SMS based chat application using some Telco APIS. I am receiving SMS only from a particular number and I want to know how I can prevent SMS being sent to the the default inbox of android.
Following is the code of my SMS receiver.
Compile SDK: 27
public void onReceive(Context context, Intent intent) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
this.intent = new Intent(BROADCAST_ACTION);
this.context = context;
chatlogDBAdapter = new ChatlogDBAdapter(context);
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
if(senderNum.equals("77100")){
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
String timestamp = String.valueOf(System.currentTimeMillis());
String[] messagearray = message.split(",", -1);
String username = messagearray[0];
String messagebody = messagearray[1];
chatlogDBAdapter.addMessage(username,username,"username",messagebody,timestamp);
msgusername = username;
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 10);
}
}
abortBroadcast();
return;
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
And the manifest
<receiver
android:name=".IncomingSms"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.provider.Telephony.SMS_DELIVER" />
<action android:name="android.provider.Telephony.SMS_DELIVER_ACTION" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
As of KitKat only one app can consume received sms directly with SMS_DELIVER_ACTION
: changelog blog here.
Any filters for the SMS_RECEIVED_ACTION broadcast in existing apps will continue to work the same on Android 4.4, but only as an observer of new messages, because unless your app also receives the SMS_DELIVER_ACTION broadcast, you cannot write to the SMS Provider on Android 4.4.