I want to receive a sms in my app, but I don't want my Android to show a notification about that event.
i was declare
<receiver android:name="mypackage.SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
written code
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
Object[] pdus = (Object[])extras.get("pdus");
for (Object pdu: pdus)
{
SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);
String origin = msg.getOriginatingAddress();
String body = msg.getMessageBody();
// Parse the SMS body
if (isMySpecialSMS)
{
// Stop it being passed to the main Messaging inbox
abortBroadcast();
}
}
}
}
and try set priority
<intent-filter android:priority="100">
But not work!Phone display notification!
have Android 4.4.2 (API 19) (Samsung Note 3)
Starting with KitKat (4.4), your app will need to be the default SMS app in order to suppress Notifications, as the default app is responsible for issuing them. Attempting to abort the SMS_RECEIVED broadcast no longer works. Any attempt to abort that broadcast is ignored by the system. Furthermore, the default app listens for the SMS_DELIVER broadcast, which cannot be aborted either, as it is delivered to only the default app. You can consult the following link for information on making your app eligible to be a default SMS app.
Getting Your SMS Apps Ready for KitKat
Related
I am trying to capture the SMS received on the phone, but when the phone receives an SMS message the method 'onReceive' is not called. This is my code:
I have the BroadcastReceiver is declared in the 'AndroidManifest.xml' inside the tag 'application':
<receiver android:name=".util.IncomingSmsReceiver"
android:exported="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
This is the IncomingSmsReceiver.java
public class IncomingSmsReceiver extends BroadcastReceiver {
public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive executed");
if (intent.getAction().equals(SMS_RECEIVED)) {
...
}
}
}
I'm doing the tests on an emulator Google Nexus 5 with Android 6. When I send a sms (fake) in the emulator a notification appears as if it was received really well and I can use it in the default application that brings the emulator. In the logcat of Android Studio does not appear that you have run the method onReceive, or the code written inside.I've tried to change the priority, I've tried using android:enabled="true", I've tried using registerReceiver and I have not gotten it to work. Does anyone know if I miss something?
Are you using a default messaging app with "Disable other apps" flag on?
Please see this:
"android.provider.Telephony.SMS_RECEIVED" not working on my device (HTC Wildfire) - how to debug?
Edit:
Since you are using Android 6, you should use the new permissions model. Check this out:
http://developer.android.com/training/permissions/requesting.html
I'm looking for an app that disables sending or receiving or both - of SMS messages from a android device . Once the app is installed it checks a web url for a boolean value 0 or 1, if it's zero, sms is disabled, else it's enabled.
Can anyone guide me from where to start ?Is this possible?
You can register a BroadcastReceiver for incoming SMS and use this.abortBroadcast(); to abort its further processing:
Inside you manifest:
<receiver android:name=".SMSBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
Implementation of the receiver:
public class SMSBroadcastReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == SMS_RECEIVED) {
Bundle bundle = intent.getExtras();
...
this.abortBroadcast();
}
}
}
However - it is not possible to abort an outgoing SMS that way. Please note that if more than one BroadcastReceivers are registered, the one with the highest priority (android:priority) will be processed first.
Imho you cant disable SMS receiving/sending at all, but you can catch the SMS received Broadcast Intent.
The SMS received Broadcast Intent is a ordered Broadcast, it will be delivered to all Broadcast Receivers in order of the Priority defined in the Receiver.
If you register a Broadcast Receiver with Priority 0 you can catch the SMS Broadcast and cancel it.
Keep in mind that other SMS Apps can also register with Priority 0 the behavior is undefined for same priority values. Also the SMS is received anyway, you only hide the Resulting Notifications/Ringtone.
Another approach would be to set the SMS Central Number to some invalid Values, required a rooted Phone though
I am working on a sms blocker application, in which i am using broadcast receiver and abortBroadcast() method - as many ppl have suggested here - to prevent messages from reaching inbox and alerting the user. But in my case, when I send a sms using the emulator, the SMS message won't get blocked, and reaches the inbox, also I get an error :
06-29 09:19:05.854: E/BroadcastReceiver(868): BroadcastReceiver trying to return result during a non-ordered broadcast
which doesn't terminate the app in the emulator, however the application gets terminated when I test it on my phone.
And yes, I have set the receiver's priority to a high number and asked for the permissions as you see here:
<receiver android:name="SMSMonitor">
<intent-filter android:priority="9999999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RAISED_THREAD_PRIORITY"/>
Finally, here's my code:
public class SMSMonitor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean isOn = loadState(context,"isOn");// is blocking enabled?
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
String mAddress;
String mBody;
String mTime;
if(isOn){
// if spam blocking is enabled.
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
mAddress=smsMessage[n].getDisplayOriginatingAddress().toString();
mBody=smsMessage[n].getDisplayMessageBody().toString();
mTime=getTime();
if(isBlackList( mAddress)== true) {
this.addLog(mAddress, mBody, mTime);
abortBroadcast();
Toast.makeText(context,"Incoming SMS was blocked and logged.", Toast.LENGTH_LONG).show();
}
}
}
}
}
Some one suggested here that the SMS broadcasts can't be aborted because Android won't allow it. But I have seen many guys here suggested using abortBroadcast() to block a sms, and also I know some SMS blocker apps on the market that actually CAN block SMSs. I don't know if they are using abortbroadcast or not.
Any ideas?
Definitely you can do this, code that you have written is perfectly fine,
Try changin priority from <intent-filter android:priority="9999999"> to <intent-filter android:priority="99999999"> , basically higher priority then what you have given, then try to check.
Is it showing the Toast from receiver? Probably your Receiver is not registered properly in Manifest.
try to change,
<receiver android:name="SMSMonitor">
to
<receiver android:name=".SMSMonitor">
"As of Android 1.6, incoming SMS message broadcasts (android.provider.Telephony.SMS_RECEIVED) are delivered as an "ordered broadcast" — meaning that you can tell the system which components should receive the broadcast first." and I am using Android 1.5 And the broadcast is non-ordered!
Thanks to this guy here link
Hi I want to send message from one application (which will be installed on android mobile) to
another application (which will be installed on another mobile). This means
One mobile ------------sends message-------- >to another mobile.
Similarly I want second mobile to send----------message ----- to first mobile.
If I use sms to send message I think it will be saved in the sms box and so the user can read
the message. I want to hide the message . So is there any way I can send message directly from
one application to another.
If you want to prevent saving sms to inbox, you can abort broadcast:
public class SmsReceiver extends BroadcastReceiver {
// ...
#Override
public void onReceive(Context context, Intent intent) {
// ...
if( smsIsMine() ) {
// Do something with sms
this.abortBroadcast();
}
}
}
You also need to set the priority of SmsReceiver the highest possible priority in your AndroidManifest.xml as below:
<receiver
android:name=".SmsReceiver">
<intent-filter android:priority="10" >
<!-- ... -->
</intent-filter>
</receiver>
I want to know about intercepting an incoming SMS message for a specific key word; for example, "Hi", so that I can read that sms containing "Hi" in it and delete it after reading the message, and if that message doesn't contain any such text then it wouldn't be deleted and instead saved in the inbox.
Look for Broadcast Receiver, this is dependent on the apps installed on the phone but you can give your app priority for listening to messages. Although, when a notification is shown, the message won't be in the SMS Database yet, so you will need to use abortBroadcast() to stop other apps being notified. See example below:
public class MessageReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i(TAG, messages.getMessageBody());
if(messages.getMessageBody().contains("Hi")) {
abortBroadcast();
}
}
And you would need to declare the receiver in the manifest, like so:
<receiver android:name="com.encima.smsreceiver.MessageReceiver" android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
Finally, make sure you have the permission to RECEIVE_SMS in the manifest.
As of Android 4.4, what you ask to do in the question is impossible
Since Android version 4.4, If your app is not the default messaging app, you won't be able to perform those things promised in the accepted answer. You can neither stop the default messaging app from getting notified, nor you can delete an sms message.