I have two application with two different data sms broadcast and I want to receive data sms in both of them. For that I have created broadcast in each app
Receiver one in app 1:
<receiver android:name=".SMSReceiver" >
<intent-filter android:priority="10" >
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="5555" />
</intent-filter>
</receiver>
Receiver two in app two:
<receiver android:name=".SMSReceiver1" >
<intent-filter android:priority="10" >
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>
Now the scenario is this, sms received only in first installed app, I have tried with same ports, class name and also with different. But problem not solved, How I can solve it?? Here is my broadcast receiver class
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle myBundle = intent.getExtras();
SmsMessage[] messages = null;
String strMessage = "";
byte[] data = null;
if (myBundle != null) {
Object[] pdus = (Object[]) myBundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
phoneNo = messages[i].getOriginatingAddress();
try {
data = messages[i].getUserData();
} catch (Exception e) {
}
if (data != null) {
for (int index = 0; index < data.length; ++index) {
strMessage += Character.toString((char) data[index]);
}
}
}
message = strMessage;
System.out.println("message received: " + message);
}
}
and my code for sending message
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendDataMessage(myNumber, null,(short)8901, sms.getBytes(), null, null);
to receive sms from a different port, set this receiver in your AndroidManifest.xml
<receiver android:name=".SMSBcastReceiver">
<intent-filter android:priority="10">
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="13013" />
</intent-filter>
</receiver>
then set this in your SMSBcastReceiver class
public void onReceive(final Context context, Intent intent) {
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]);
}
smsOrigin = smsMessage[0].getDisplayOriginatingAddress();
smsData = smsMessage[0].getUserData();
for(int index=0; index<smsData.length; ++index)
{
smsBody += Character.toString((char)smsData[index]);
}
}
Couple of things to double check:
a) Are you sure you have added android.permission.RECEIVE_SMS
permission in both applications?
b) Can you remove this piece from both manifests to make sure that intent filtering doesn't mess up?
<data android:scheme="sms" />
<data android:port="5555" />
c) If b) will work, you may be interested to do filtering in your Receiver code.
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