receive SMS from a port [duplicate] - android

I've found a few tutorials on how to send/receive text SMS messages, but none on how to send/receive data SMS messages. I have a very small amount of data I would like the users of my app to be able to share.
I am able to send, but my BroadcastReceiver doesn't ever get called. It seems this is a known issue (http://code.google.com/p/android/issues/detail?id=1576) but has anyone figured out how to do this yet?
I tried sending/receiving a text SMS and that works fine, the thing is, I need to specify a port so only my app can listen for the SMS.
It seems this question has been asked here before and was never answered: how to receive text sms to specific port..

I know this is 1 year old at time of my response, but I thought it could still help someone.
Receiving:
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0; i<pdus.length; i++){
recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);
try {
data = recMsg.getUserData();
} catch (Exception e){
}
if (data!=null){
for(int index=0; index<data.length; ++index)
{
recMsgString += Character.toString((char)data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
Setting up Receiver in Manifest:
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>
Sending:
String messageText = "message!";
short SMS_PORT = 8901; //you can use a different port if you'd like. I believe it just has to be an int value.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendDataMessage("8675309", null, SMS_PORT, messageText.getBytes(), null, null);

Related

android: Data message with port is not receiving

I'm using a port for sending Data message in my app.
I have a problem with sending data message (sendDataMessage in android). my code works properly with MCI sim cards, I mean when the sender and receiver have MCI sim cards.
If sender has a MCI and receiver has an Irancell sim card message delivers properly.
On the contrary, if sender has an Irancell sim and receiver has a MCI sim it is not delivered at all :( (although in this case text message send/receive works properly, data message won't work).
Moreover when sender and receiver have Irancell sims it works properly.
please help me with this problem.
My manifest code:
<receiver android:name=".SmsListener">
<intent-filter android:priority="999">
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<data android:scheme="sms" />
<data android:port="7442" />
</intent-filter>
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<meta-data android:name="port" android:value="7442" />
My smsListener:
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Yuhuuu", Toast.LENGTH_LONG).show();
MainActivity.smsReceived(true);
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {
recMsg = SmsMessage.createFromPdu((byte[]) pdus[i]);
try {
data = recMsg.getUserData();
} catch (Exception e) {
}
if (data != null) {
for (int index = 0; index < data.length; ++index) {
recMsgString += Character.toString((char) data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
}
System.out.println(recMsgString + " ,this Has been Recieved from: " + fromAddress);
}
and my DataMessage sender code:
String messageText = "some text";
short SMS_PORT = 7442;
SmsManager smsManager = SmsManager.getDefault();
String phoneNumber = "+98910*******";
smsManager.sendDataMessage(phoneNumber, null, shortValue, messageText, null, null);
}

Android - sendTextMessage Message not showingup in SMS Apps

I am writing an app which will send an SMS automatically to a given number on some conditions, SMS code is working and SMS is also delivered.. but the sent SMS are not showing up in any of the other SMS Clients/Apps installed...
String phoneNumber = "1234512345";
String message = "Test Message";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
What am i missing?
I want the sent SMS (sent by my app) to show up in other SMS apps which are installed..
Yes you have sent SMS progrmatically but you haven't informed SMS Content Provider so it will not get your message unless you inform it.
ContentValues values = new ContentValues();
values.put("address", "1234512345");
values.put("body", "Test Message");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
Make sure to include below permissions in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
use broadcast receiver in receiving side application
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
String str="";
String from="";
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += messages[i].getMessageBody().toString();
from = messages[i].getOriginatingAddress().toString();
}
if(from.equals("123456789")){
str= str.trim();
abortBroadcast();
Intent intt = new Intent(context,MainActivity.class);
intt.putExtras("message",str);
intt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intt);
}
}
in your mainActivity write some code to display the obtained message. the message will be in the intent. so get the String from it.
here the broadcast receiver, receives messages from only number "123456789". the messages from this number will not go to inbox of ur mobile. if u want to see in inbox also remove the method abortBroadcast().

Unable to receive sms in my android application

I am trying to receive the sms from a particular number in my android application and trying to show the message content in my application. But i am not able receive the sms.
the class which i am using to receive is given below.
public class SmsReceiver extends BroadcastReceiver {
StringBuilder sb;
#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.
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("********")) {
// Process our sms...
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS");
broadcastIntent.putExtra("data", sb.toString());
context.sendOrderedBroadcast(broadcastIntent, null);
System.out.println("MESSAGE FROM SERVER -->"
+ sb.toString());
this.abortBroadcast();
}
return;
}
}
}
}
After receiving the sms, i will check the sender and if it is the sender i am looking for then i will send another broadcast with the sms. There i will show the content.
see my manifest
<receiver android:name=".SmsReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="SMS" />
</intent-filter>
</receiver>
I am not able to find a solution please help.
Things to check (from my experience):
android:priority="2147483647" is not valid, the largest value is 1000. Numbers larger than this are ignored
Insert your this.abortBroadcast(); in the for loop (to be called pdus.length times)
If the sent SMS matches your criteria, for testing, save it in your app preference and in your main activity read the value from your preference, if it worked, then the problem is with your broadcastIntent.

How to get Cell Broadcast message?

I try to get text of Cell Broadcast message just like sms, but it does'not work:
public class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
// ---retrieve the SMS message received---
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 =msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
Do you know any way to get it?
I also spent some time on investigation of this question. And it seems that there is no public API to do that. But I can share some results from my reverse engineering research...
My Samsung Galaxy S is able to receive CB messages, so I decompiled SMS app and looked into the code. It has the following BroadcastReceiver in its manifest file:
<receiver android:name=".transaction.PrivilegedSmsReceiver">
...
<intent-filter>
<action android:name="android.provider.Telephony.CB_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.CB_SETTINGS_AVAILABLE" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.SET_CB_ERR_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.GET_CB_ERR_RECEIVED" />
</intent-filter>
</receiver>
Note the android.provider.Telephony.CB_RECEIVED intent-filter. I did not find any documentation about it, but from its name I assumed that it's the only broadcast that I need to catch for now.
Then I searched through the code of decompiled apk and found that it uses android.provider.Telephony.Sms.Intents->getCbMessagesFromIntent() interface to access retrieve CB messages, which returns CbMessage class instance. This interface is outdated even for simple SMS messages, so I assumed that CbMessage should work with pdus as SmsMessage does. Finally I found the source of SmsCbMessage class which is pretty similar to SmsMessage by API. It depends on 5-6 internal Android java files, so for simplicity I just grab them from the same site and included them into my project.
The broadcastReceiver is the same as yours except the class SmsMessage is replaced by SmsCbMessage:
public class CbReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//---get the CB message passed in---
Bundle bundle = intent.getExtras();
SmsCbMessage[] msgs = null;
String str = "";
if (bundle != null) {
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsCbMessage[pdus.length];
for (int i=0; i<msgs.length; i++) {
msgs[i] = SmsCbMessage.createFromPdu((byte[])pdus[i]);
str += "CB lang " + msgs[i].getLanguageCode();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new CB message---
abortBroadcast();
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
After installing my application into my SGS phone with the receiver above, and enabling receiving CB messages in phone SMS application, my app was able to show CB messages in toast in parallel with receiving them by standard SMS application.
The issues are still needed to be resolved:
How to enable/disable/configure_channels of CB messages in my
application? SMS app uses getCbSettings()/setCbSettings() functions,
but I did not find them. So temporarily I used other app for that.
How to
abort CB message broadcast, so other apps do not receive them? It
seems abortBroadcast() does not work here, because the broadcast
message is not ordered (isOrderedBroadcast() returns false).

How to send and receive data SMS messages

I've found a few tutorials on how to send/receive text SMS messages, but none on how to send/receive data SMS messages. I have a very small amount of data I would like the users of my app to be able to share.
I am able to send, but my BroadcastReceiver doesn't ever get called. It seems this is a known issue (http://code.google.com/p/android/issues/detail?id=1576) but has anyone figured out how to do this yet?
I tried sending/receiving a text SMS and that works fine, the thing is, I need to specify a port so only my app can listen for the SMS.
It seems this question has been asked here before and was never answered: how to receive text sms to specific port..
I know this is 1 year old at time of my response, but I thought it could still help someone.
Receiving:
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0; i<pdus.length; i++){
recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);
try {
data = recMsg.getUserData();
} catch (Exception e){
}
if (data!=null){
for(int index=0; index<data.length; ++index)
{
recMsgString += Character.toString((char)data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
Setting up Receiver in Manifest:
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>
Sending:
String messageText = "message!";
short SMS_PORT = 8901; //you can use a different port if you'd like. I believe it just has to be an int value.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendDataMessage("8675309", null, SMS_PORT, messageText.getBytes(), null, null);

Categories

Resources