How to prevent SMS going to inbox in Android? - android

I'm developing a business SMS application. In this app, if an incoming message is from a particular number, say 999999999, it should go to the application's inbox and not to the default native inbox. All other messages should go to the phone's native inbox. How do I do this?

When SMS is received by the Android system, it broadcasts an ordered broadcast Intent with action "android.provider.Telephony.SMS_RECEIVED". All registered receivers, including the system default SMS application, receive this Intent in order of priority that was set in their intent-filter. The order for broadcast receirers with the same priority is unspecified. Any BroadcastReceiver could prevent any other registered broadcast receivers from receiving the broadcast using abortBroadcast().
So, everything you need is broadcast receiver like this:
public class SmsFilter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[])extras.get("pdus");
if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.
StringBuilder sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (sender == null) sender = message.getOriginatingAddress();
String text = message.getMessageBody();
if (text != null) sb.append(text);
}
if (sender != null && sender.equals("999999999")) {
// Process our sms...
abortBroadcast();
}
return;
}
}
// ...
}
}
Looks like the system default SMS processing application uses priority of 0, so you could try 1 for your application to be before it. Add these lines to your AndroidManifest.xml:
<receiver android:name=".SmsFilter">
<intent-filter android:priority="1">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Don't forget about necessary permissions:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
By the way, you can find all registered receivers and their priorities using this code:
Intent smsRecvIntent = new Intent("android.provider.Telephony.SMS_RECEIVED");
List<ResolveInfo> infos = context.getPackageManager().queryBroadcastReceivers(smsRecvIntent, 0);
for (ResolveInfo info : infos) {
System.out.println("Receiver: " + info.activityInfo.name + ", priority=" + info.priority);
}
Update: As FantasticJamieBurn said below, starting from Android 4.4 the only app that can intercept SMS (and block if it wish) is the default SMS app (selected by user). All other apps can only listen for incoming SMS if default SMS app not blocked it.
See also SMS Provider in the Android 4.4 APIs.

With the release of Android 4.4 KitKat (API level 19), the option to block an SMS message and prevent it from being delivered to the default SMS app has been removed. Non-default SMS app's may observe SMS messages as they are received, but any attempt to abort the broadcast will be ignored by Android 4.4+.
If you have an existing app which relies on aborting SMS message broadcasts then you may want to consider the impact this change in behaviour will have when your users upgrade to Android 4.4+.
http://android-developers.blogspot.co.uk/2013/10/getting-your-sms-apps-ready-for-kitkat.html

Yes it can be DOne
public void onReceive(Context context, Intent intent)
{
Bundle bundle=intent.getExtras();
Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] sms=new SmsMessage[messages.length];
Toast.makeText(context, "Hello", 1).show();
for(int n=0;n<messages.length;n++){
sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
}
for(SmsMessage msg:sms){
if(msg.getOriginatingAddress().endsWith(number))
{
SMS.updateMessageBox("\nFrom: "+msg.getOriginatingAddress()+"\n"+
"Message: "+msg.getMessageBody()+"\n");
/*((SMS) context).delete();*/
abortBroadcast();
}
}
}
just use abortbroadcast() after receiving in app

Are you the one sending the messages? If so consider using datasms instead as they will not show up in the inbox.
Check this question for more info on how to use it

Check the sender number is equal to the mobile number of your sms sending phone.
replace the following code line of Mr "praetorian droid"
if (sender != null && sender.equals("999999999")) {
to
if (sender != null && sender.equals("YOUR SMS SENDING MOBILE NUMBER HERE")) {
further more you can give a setting to user to manually add sms sending number if he want to change it.

Related

Broadcast receiver is not working when application is killed

I have some logic that I need to execute when the user is getting any SMS. My Broadcast receiver is working fine when the app is running but if I am killing my then it is not working as expected.
Can someone help me with this? I have tried all the possible approaches which are present on the Internet but still nothing is working.
Thank you so much in advance.
My broadcast receiver class:
public class EveryDay_SMSListener extends BroadcastReceiver {
private SharedPreferences preferences;
String msgBody ="";
String msg_from;
EveryDay_DataBase mydb;
#Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, EveryDay_Service.class);
context.startService(background);
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
enter code here
if (bundle != null){
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msgBody = msgBody+(msgs[i].getMessageBody()).toString();
msg_from = msgs[i].getOriginatingAddress();
}
}catch(Exception e){
}
}
}
}
}
In recent versions of Android, system does not launch app when app is dead
and broadcast gets emitted (for most of broadcast flags).
An approach for making this work you should use Foreground Services, some links that may help you implement it:
https://androidwave.com/foreground-service-android-example/
https://medium.com/#ramkumarv3/foreground-service-with-notification-channel-ac6697c8a6d1
https://developer.android.com/guide/components/services
Take care, some manufacturers even close these services for battery optimizations like Huawei, Samsung,... . So you should add brand specific codes to make user disallow battery optimization and automatic close of services for your app.
You said registering receiver in manifest did not work for you thus the only remaining option is:
create a foreground service and register SMS receiver in onCreate of
it (do NOT register sms receiver in manifest).
create a jobscheduler that starts your service every (n) seconds
it is the last solution but if you wont be lucky between app kill and next jobscheduler work you may lost sms so you may want to make (n) small enough.
Better solution is to have read sms permission and every time your activity starts read all sms from android inbox and see if you lost any sms.

Error in broadcast receiver for android, it listens to all SMSs that my phone receives

I'm doing an encryption SMS app, in which, user can encrypt the text and send SMS through my apps.
I used the following broadcast receiver.
The problem is that its listen to all the SMS that come to my phone.
How to make it so that it will only listen to the SMS sent from my apps? Other sms's should open as normal, using default SMS application
public class SmsBroadCastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
// Specify the bundle to get object based on SMS protocol "pdus"
Object[] object = (Object[]) bundle.get("pdus");
SmsMessage sms[] = new SmsMessage[object.length];
Intent in=new Intent(context,DisplaySMSActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String msgContent = "";
String originNum = "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < object.length; i++) {
sms[i] = SmsMessage.createFromPdu((byte[]) object[i]);
// get the received SMS content
msgContent = sms[i].getDisplayMessageBody();
//get the sender phone number
originNum = sms[i].getDisplayOriginatingAddress();
//aggregate the messages together when long message are fragmented
sb.append(msgContent);
//abort broadcast to cellphone inbox
abortBroadcast();
}
//fill the sender's phone number into Intent
in.putExtra("originNum", originNum);
//fill the entire message body into Intent
in.putExtra("msgContent", new String(sb));
//start the DisplaySMSActivity.java
context.startActivity(in);
}
Maybe you are not correctly registering your receiver.
When declaring a Receiver in the manifest (or programmatically), you can also specify an intent filter. You can specify the 'action' you want to receive in your receiver.
For example: "com.your_app_package.sms_encrypted_msg".
Either way, remember to check the action in the onReceive method:
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals('com.your_app_package.sms_encrypted_msg')){
...
}
}

Hide sms notifications with a broadcastreceiver in KitKat

I'm trying to intercept the received SMSs by using a broadcast receiver. Here is the code:
<receiver android:name=".receivers.SmsReceiver" android:enabled="true"
android:exported="true" android:priority="999">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
and:
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (SMS_RECEIVED.equals(intent.getAction())) {
this.abortBroadcast();
Bundle bundle = intent.getExtras();
if (bundle != null) {
// get sms objects
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus.length == 0) {
return;
}
// large message might be broken into many
SmsMessage[] messages = new SmsMessage[pdus.length];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(messages[i].getMessageBody());
}
String sender = messages[0].getOriginatingAddress();
String message = sb.toString();
Log.d("sms", sender);
Log.d("sms", message);
}
}
}
}
The SMS is intercepted fine, but the stock Android SMS app is still showing its notifications and I can also find the message inside the stock app sms list.
Is there any way to stop the stock SMS app notifications and to avoid the message from appearing inside its list?
You need to call abortBroadcast();, see my answer to communication between two device using sms
If you are running Android 4.4 KitKat, it seems to be more difficult to do these sorts of things and have not looked into it yet myself.
As you said you are running KitKat, then answer is - you cannot mute default SMS app. You can also receive messages or send (that's why you get messages), but still, you cannot "consume" the message.
If user has set your application as default SMS app, then he/she is not going to get an SMS notification. You have to handle the notification, as well as other feature of the SMS in your app.
For more information read this blog.
Check out other blog and sample app.

Android: Receiving SMS from specific phone number

Is it possible for my application to receive an SMS from a specific phone number, without letting it trig a system notification, but let all other messages pass to the default SMS application to be treated normally?
If so, how can the system know which process is first on queue to pick which messages to receive?
I didn't try this, but in theory it should work:
In Android SMS broadcast is sent as ordered broadcast, which means that receivers are handled in order and can cancel the broadcast. See SMSDispatcher.java, line 420.
In order to be called first, a receiver must have a higher priority then others.
<intent-filter android:priority="1000" >
. . .
</intent-filter>
To cancel a broadcast call broadcastReceiver.setResultCode(RESULT_CANCELED). That way a SMS broadcast will be cancelled and will not be shown by system SMS app (and SMS notifier).
Update:
Also try using broadcastReceiver.setResultCode(Intents.RESULT_SMS_HANDLED).
Update 2:
user672601 noted in another answer that this indeed works, but he used abortBroadcast() inside broadcast receiver.
I second farhan its not possible for number of reason. Anybody can do anything with such allowance. Check this out for details http://groups.google.com/group/android-developers/browse_thread/thread/78fecbc156f4a1ea
Peter Knego's answer is correct. I was trying to do this exact thing, tried his solution, and it indeed works except for I used:
this.abortBroadcast();
inside the broadcastReceiver.
public class NotifyServiceReceiver extends BroadcastReceiver{
static final String ACTION ="android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if(arg1.getAction().equalsIgnoreCase(ACTION))
{
Bundle extras = arg1.getExtras();
String strMessage = "private message";
if ( extras != null )
{
Object[] smsextras = (Object[]) extras.get( "pdus" );
for ( int i = 0; i < smsextras.length; i++ )
{
SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);
String strMsgBody = smsmsg.getMessageBody().toString();
String strMsgSrc = smsmsg.getOriginatingAddress();
//Toast.makeText(GasService.this,strMessage, Toast.LENGTH_SHORT).show();
if(strMsgSrc.equals("+919XXXXXXXXX"))
{
strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;
Toast.makeText(PrivatesmsService.this,strMessage, Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
}
}
}
}
you can tell to the system by setting the priority of the activity to 100 or greater then that in manifest file so that when ever you receive sms then your application will access it and by calling abortBroadcast() it will prevent sms reaching to inbox or any other application which has set BroadcastReceiver to receive sms
I dont think its possible.... because android gives us broadcast Listener which only listen the event. so you have to read every message and check the number if its yours, do an operation else just ignore it.... the default messaging application will automatically handle it....

Start application on receiving sms from particular number

i want to start an application on receiving sms from a particular number.
i am trying it with onMessageWaitingIndicatorChanged(boolean mwi){ } method but
i m struggling.
so, anyone there to help me in detail?
Thanks
You'll need to register a broadcast receiver for android.provider.Telephony.SMS_RECEIVED. The receiver can then check the number of the SMS and start your activity as appropriate.
So, you'll need to:
Add a uses-permission for android.permission.RECEIVE_SMS to your manifest
Declare a broadcast receiver in your <application/> element in the manfiest:
<receiver android:name=".YourReceiverName">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Create the receiver class, extending IntentReceiver.
In onReceiveIntent, you can get the relevant messages by calling Telephony.Sms.Intents.getMessagesFromIntent() and passing in the intent you're supplied.
If the number matches the one you want, you can then start an activity by calling startActivity
here is my onReceive() method, it works :)
public void onReceive( Context context, Intent intent ) {
// get incoming message
Bundle extras = intent.getExtras();
String messages = "";
// if message available, go on
if ( extras != null ) {
// get the array of the message
Object[] smsExtra = (Object[]) extras.get( "pdus" );
// loop through the number of available messages
for ( int i = 0; i < smsExtra.length; ++i ) {
// create smsmessage from raw pdu
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
// retrieve contents of message
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
// only accept messages from specified number
if(address.equals(0000)){
// store the message to database
storeToDatabase( contentResolver, sms );
// stop message from getting to default app
this.abortBroadcast();
}
}
}
}

Categories

Resources