I just need to get incoming SMS to handle it, but nothing is called from onRecieve method. everything looks just ok , but nothing is happened when i receive SMS!
here is my manifest tags inside application tags :
<receiver android:name="com.chargeirancell.key0098.view.RubinAppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/rubinwidgetinfo" />
</receiver>
<receiver android:name="com.chargeirancell.key0098.RubinRecieveSMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
here also are permissions outside application tag:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
this my receiver class :
public class RubinRecieveSMS extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("SMS", "HI");
if (ChargeApp.mShared.getBoolean("sms", true)) {
Log.i("SMS", "HI");
String number = "";
String message = "";
Bundle bundle = intent.getExtras();
if (bundle != null) {
try {
int i;
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] currentMessages = new SmsMessage[pdusObj.length];
for (i = 0; i < currentMessages.length; i++) {
currentMessages[i] = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
number = currentMessages[i].getOriginatingAddress();
message = message
+ currentMessages[i].getMessageBody();
}
String pin;
if (number.contains("3453")) {
pin = message.substring(message.indexOf("ز:"),
message.indexOf("ک")).replace("ز:", "");
Intent i1 = new Intent(context,
ActivityDialogCharge.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.putExtra("pin", pin);
context.startActivity(i1);
} else if (number.contains("8801 9574")) {
pin = message.substring(message.indexOf("ز:"),
message.indexOf("ک")).replace("ز:", "");
Intent i1 = new Intent(context,
ActivityDialogCharge.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.putExtra("pin", pin);
context.startActivity(i1);
}
} catch (Exception e) {
}
}
}
}
}
even Hi is not printed in logCat!
can anyone please help me on this?
Replace code into your manifest file:
<receiver android:name=".RubinRecieveSMS" android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Related
I am developing an SMS/MMS messaging system and I need the app to receive both SMS and MMS messages. It receives SMS messages just fine. I am testing on a real phone (Galaxy S7 Edge running 7.01).
The app does not receive MMS messages. According to this post, my manifest is correct:
<receiver android:name="com.webnation.text2email.receivers.MMSBroadcastReceiver"
android:enabled="true"
android:permission="android.permission.BROADCAST_WAP_PUSH"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
However, setting a break point at the first line of the broadcast receiver, the broadcast receiver is never called. I am including all the right permissions.
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="com.android.vending.BILLING" />
My broadcast receiver:
public class MMSBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_DELIVER";
private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";
// Retrieve MMS
public void onReceive(Context context, Intent intent) {
this.context = context; <------- break point set here.
String action = intent.getAction();
String type = intent.getType();
int typeMessage = -1;
byte[] pushData;
GenericPdu pdu = null;
Toast.makeText(context, "MMS received", Toast.LENGTH_LONG).show();
if (action.equals(ACTION_MMS_RECEIVED) && type.equals(MMS_DATA_TYPE)) {
Bundle bundle = intent.getExtras();
Timber.d("bundle " + bundle);
if (bundle != null) {
pushData = intent.getByteArrayExtra("data");
PduParser parser = new PduParser(pushData);
pdu = parser.parse();
PduHeaders headers = pdu.getPduHeaders();
Timber.d("buffer " + pushData);
String incomingNumber = new String(pushData);
int indx = incomingNumber.indexOf("/TYPE"); // not used, parse the number in MMUtils.getAddress instead
if (indx > 0 && (indx - 15) > 0) {
int newIndx = indx - 15;
incomingNumber = incomingNumber.substring(newIndx, indx);
char[] characters = incomingNumber.toCharArray();
int index = 0;
for (int i=0;i<characters.length;i++) {
if (Character.isDigit(characters[i]) ){
index = i;
break;
}
}
//indx = incomingNumber.indexOf("+");
if (index > 0) {
incomingNumber = incomingNumber.substring(index);
TelephonyManager tm = (TelephonyManager)context.getSystemService(context.getApplicationContext().TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso().toUpperCase();
try {
String prefix = CountryToPhonePrefix.prefixFor(countryCode).replace("+", "");
int indexOfBaseNumber = incomingNumber.indexOf(prefix);
if (indexOfBaseNumber > -1) {
if (indexOfBaseNumber == 0) {
int lengthPrefix = prefix.length();
indexOfBaseNumber = indexOfBaseNumber + lengthPrefix;
}
incomingNumber = incomingNumber.substring(indexOfBaseNumber);
}
} catch (IllegalArgumentException ie) {
Timber.e(ie);
} catch (IndexOutOfBoundsException ie) {
Timber.e(ie);;
}
Timber.d("Mobile Number: " + incomingNumber);
}
}
int transactionId = bundle.getInt("transactionId", -1);
Timber.d("transactionId " + transactionId);
int pduType = bundle.getInt("pduType", -1);
Timber.d("pduType " + pduType);
byte[] buffer2 = bundle.getByteArray("header");
if (buffer2 != null) {
String header = new String(buffer2);
Timber.d("header " + header);
}
List<MMSMessage> textMessages = MMSUtils.getMessagesFrom(context, intent); //meat of onReceive()
sendMessages(textMessages);
} else {
Timber.e("Invalid PUSH data");
}
}
}
}
What am I doing wrong?
Adding the following to my main activity in the Manifest seems to have solved the problem:
<activity
android:name="com.webnation.text2email.main.SMSEmailActivity"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I know from KitKat onward we need to make out app as default SMS app to be able to modify the SMS database.
But I don't want that. I only need to get notification of incoming SMS and display as toasts, and also send SMS.
Do I need to make my app default for that?
Edit 1:
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rtrgroup.sms_to_neta_server"
android:installLocation="preferExternal">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginScreen" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"/>
<activity android:name=".MainScreen" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
</activity>
<service android:name=".MainService"
android:exported="true">
</service>
<receiver android:name=".MainReceiver"
android:exported="true"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my BroadcastReceiver:
package com.rtrgroup.sms_to_neta_server;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.widget.Toast;
import android.telephony.*;
public class MainReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage;
if (Build.VERSION.SDK_INT >= 19) { //For ver. >= KitKat
SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
currentMessage = msgs[0];
} else {
Object pdus[] = (Object[]) bundle.get("pdus");
currentMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
}
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String messageB1 = currentMessage.getDisplayMessageBody();
String msg = currentMessage.getMessageBody();
String address = currentMessage.getOriginatingAddress();
Toast.makeText(context, senderNum + ": " + msg, Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
}
}
}
}
You don't need to be the default SMS app to send SMS. SmsManager.sendTextMessage() still works as long as you have the android.permission.SEND_SMS permission.
To receive SMSs, you can monitor the android.provider.Telephony.SMS_RECEIVED broadcast, with android.permission.RECEIVE_SMS permission.
Ref. http://android-developers.blogspot.fi/2013/10/getting-your-sms-apps-ready-for-kitkat.html
I am trying to send user to user notification on click of a button to specific users but getting this issue while sending notification from last 4 days in android, and with this, whenever i uninstall the app from my phone which i am using as emulator, and reinstall it for debugging, it doesnt create new installation id and take the old one: i Guess this is the problem, please it will be great help if anybody help me in a elaborated way
Here is my code of custom reciever:
public class CustomPushReceiver extends ParsePushBroadcastReceiver {
private final String TAG = CustomPushReceiver.class.getSimpleName();
private NotificationUtils notificationUtils;
private Intent parseIntent;
public CustomPushReceiver() {
super();
}
#Override
protected void onPushReceive(Context context, Intent intent) {
super.onPushReceive(context, intent);
if (intent == null)
return;
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.e(TAG, "Push received: " + json);
parseIntent = intent;
parsePushJson(context, json);
} catch (JSONException e) {
Log.e(TAG, "Push message json exception: " + e.getMessage());
}
}
#Override
protected void onPushDismiss(Context context, Intent intent) {
super.onPushDismiss(context, intent);
}
#Override
protected void onPushOpen(Context context, Intent intent) {
super.onPushOpen(context, intent);
}
/**
* Parses the push notification json
*
* #param context
* #param json
*/
private void parsePushJson(Context context, JSONObject json) {
try {
boolean isBackground = json.getBoolean("is_background");
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
String message = data.getString("message");
if (!isBackground) {
Intent resultIntent = new Intent(context, MainActivity.class);
showNotificationMessage(context, title, message, resultIntent);
}
} catch (JSONException e) {
Log.e(TAG, "Push message json exception: " + e.getMessage());
}
}
/**
* Shows the notification message in the notification bar
* If the app is in background, launches the app
*
* #param context
* #param title
* #param message
* #param intent
*/
private void showNotificationMessage(Context context, String title, String message, Intent intent) {
notificationUtils = new NotificationUtils(context);
intent.putExtras(parseIntent.getExtras());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, intent);
}
}
and my ParseQuery:
try {
ParseQuery userQuery = new ParseQuery(ParseUser.class);
userQuery.whereNear("Location",
currentUser.getParseGeoPoint("Location"));
userQuery.setLimit(10);
userQuery.whereEqualTo("Type", "Doctor");
userQuery.whereEqualTo("deviceType", "android");
userQuery.findInBackground();
// Find devices associated with these users
ParseQuery pushQuery = ParseInstallation.getQuery();
pushQuery.whereMatchesQuery("user", userQuery);
// Send push notification to query
ParsePush push = new ParsePush();
push.setQuery(pushQuery);
push.setMessage("Only users near"
+ currentUser.getParseGeoPoint("Location")
+ " will recieve this push notification");
push.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(),
"Notification Sent", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getApplicationContext(),
"Notification not Sent",
Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aspire.wecare"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.aspire.wecare.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.aspire.wecare.permission.C2D_MESSAGE" />
<permission
android:name="com.aspire.wecare.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.aspire.wecare.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:name="com.aspire.wecare.ParseApp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.aspire.wecare.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.aspire.wecare.Welcome"
android:label="#string/title_activity_welcome" >
</activity>
<activity
android:name="com.aspire.wecare.LoginActivity"
android:label="#string/title_login" >
</activity>
<activity
android:name="com.aspire.wecare.LoginSignupActivity"
android:label="#string/title_activity_login_signup" >
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBA3_YfEl2cnZ8uDmrI23g8W7l1y2Xiku8" />
<!-- Added for Parse push notifications -->
<service android:name="com.parse.PushService" />
<receiver
android:name=".receiver.CustomPushReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.aspire.wecare" />
</intent-filter>
</receiver>
<!-- /Added for Parse push notifications -->
</application>
</manifest>
In general terms,
(1) you would be doing this in cloud code. Fortunately it's very easy once you get started. In fact here is a total explanation from a similar question.
https://stackoverflow.com/a/32540054/294884
Muhc more importantly though,
(2) be aware you're using the wrong tool for the job. You do not use 'Push' for what you are trying to achieve. You should be using PubNub, Firebase, Pusher or a similar service. It is commonplace to use one of these in combination with Parse.
I have a Program that is sending a SMS to a device witch answers with an SMS.
I want to receive the SMS in an give the message back to my program.
Cause I will only wait for a certain time I need a dynamic registration
Here my Code for the Breadcast Receiver
public class SMSReceiver extends BroadcastReceiver {
private MainActivity father = null;
private String call_number = null;
public void setFather (MainActivity father) {
this.father = father;
call_number = father.getCallNumber().trim();
while (call_number.startsWith("0")) {
call_number = call_number.substring(1);
}
}
public void onReceive(Context cxt, Intent intent)
{
//if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
String sender = smsMessage[0].getDisplayOriginatingAddress();
String mess = smsMessage[0].getDisplayMessageBody();
String toast = "Received SMS from: " + sender + "\nMessage: " + mess;
//if ((sender != null) && (sender.endsWith(call_number))) {
father.setSMSMessage((mess ==null ? "": mess)+ "\n from "+sender);
//}
Toast.makeText(cxt, toast, Toast.LENGTH_LONG).show();
}
}
}
Here my Code for the Registration
myreceiver=new SMSReceiver();
myreceiver.setFather(this);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(myreceiver, filter);
Here my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.standheizung"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SMSChecker">
<intent-filter>
<action android:name="SMS_SENT" />
<action android:name="SMS_DELIVERED" />
</intent-filter>
</receiver>
</application>
</manifest>
My problem is that I get a SMS from the Device but my receiver does nothing.
Comment the Receiver for Sending the SMS works without any Problem
Has anybody an idea whats the problem ???
I am trying to develop an app in android which blocks an incoming sms. I have set the priority but its not blocking the incoming sms. I have used this.abortBroadcast() also but no result.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
#SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
this.abortBroadcast();
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
}
}
and the manifest file is like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="BVB.EDU"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SMS"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<receiver android:name=".SMSReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
Here's what I use for blocking incoming texts.
SmsReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class BroadCastReceiver extends BroadcastReceiver
{
/** Called when the activity is first created. */
private static final String ACTION = "android.provider.Telephony.SEND_SMS";
public static int MSG_TPE=0;
public void onReceive(Context context, Intent intent)
{
String MSG_TYPE=intent.getAction();
if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED"))
{
// Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
// toast.show();
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
// show first message
Toast toast = Toast.makeText(context,"BLOCKED Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
else if(MSG_TYPE.equals("android.provider.Telephony.SEND_SMS"))
{
Toast toast = Toast.makeText(context,"SMS SENT: "+MSG_TYPE , Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
else
{
Toast toast = Toast.makeText(context,"SIN ELSE: "+MSG_TYPE , Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="APP.PACKAGE.NAMEHERE"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true" />
<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".APPACTIVITYHERE"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden" >
<service android:name=".MyService" android:enabled="true"/>
<receiver android:name="SmsReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_SENT"/>
</intent-filter>
</receiver>
<service android:name=".MyServiceSentReceived" android:enabled="true"/>
<receiver android:name="SmsReceiver">
<intent-filter android:priority="2147483645">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
The main thing to take away from the manifest is the service block, receiver block, and the permissions.
Add abortBroadcast(); in the if(bundle!=null){} block. that should stop it going to other apps. And I noticed that your Broadcast Receiver's name is SmsReceiver, but in Manifest, you gave it ".SMSReceiver" (case sensitive).
Problem is there in your manifest, you're closing <application> tag before the receiver tag and it's wrong. All components should be inside an application tag.
Your class name is SmsReceiver, and in manifest you declared as SMSReceiver,
so you won't get the broadcast at all for your receiver.
Use have to change your class name in manifest like below
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
And in your receiver, it's better to check the intent object for whether you got the message or not and then you can abort it.
But be careful it will abort all messages. If you want abort messages depending on some particular string you can do some manipulation on message what you got and then you can abort it.
If you want to abort all messages you can directly call abortBroadcast() before doing any manipulation on that message.
Bundle extras = intent.getExtras();
if ( extras != null )
{
// do you manipulation on String then if you can abort.
if(somecondition){
abortBroadcast();
}
}
Here is my manifest which working fine, once compare with your code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application android:icon="#drawable/icon" android:enabled="true" android:label="#string/app_name">
<service android:name="com.mypackage.service.MyService" android:exported="true">
</service>
<receiver android:name="com.mypackage.receiver.MyReceiver">
<intent-filter android:priority="100"><action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter>
</receiver>
</application>
</manifest>
and the java code
public void onReceive( Context context, Intent intent )
{
if(intent != null){
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if ( extras != null ){
//read sms
}
}
}
}
#sankar
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="BVB.EDU"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SMS"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
Because of the security policy in Android 4.4 and up
to block the incoming SMS-messages it is required to give to the
application the permissions of "Default SMS-application"