I am working on an SMS receiver module in my app in which I am receiving an sms with my app, and if sms is more than 160 characters than I have to concate that SMS with its next part and display it. Currently I am working with a simple receiver code. Please suggest me to perform this task.
Yes, try as handle multipart messages :
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
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]);
}
StringBuffer content = new StringBuffer();
if (messages.length > 0) {
for (int i = 0; i < messages.length; i++) {
content.append(messages[i].getMessageBody());
}
}
String mySmsText = content.toString();
}
}
}
Related
How can i handle multipart sms in broadcast receiver and store it in one string before start operation on it like i want to store multipart sms in one string and then split it with different delimiter and then get status codes from it i have 1 sms with 250 characters . i tried with below code but not working i tested it on emulator kindly help me i also put code for receiving.
String mySmsText ;
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
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]);
}
StringBuffer content = new StringBuffer();
if (messages.length > 0) {
for (int i = 0; i < messages.length; i++) {
content.append(messages[i].getMessageBody());
}
}
mySmsText = content.toString();
}
instance.t1.setText(mySmsText);
}
}
i tried to print msg but it display bad characters also overrides old one.
I'm sorry in first there is no problem with my code the problem is only with emulator on which i was tested. when i test it on real device it work well i'm sorry ...
Finally i done it thanx mr AxelH for your answer
DataBaseHandler db;
String mySmsText;
public void onReceive(Context context, Intent intent) {
db = new DataBaseHandler(context);
Bundle bundle = intent.getExtras();
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]);
if (messages.length > 0) {
StringBuffer content = new StringBuffer();
for (SmsMessage sms : messages)
content.append(sms.getDisplayMessageBody());
mySmsText = content.toString();
}
db.update_sys_pwd(mySmsText);
instance.t1.setText(mySmsText);
}
}
I've a sms listener that read sms from a certain number . It reads the sms without any problem but there is a bug here , if the message body is very big and it goes into 2 messages, the listener only detect the first one and cannot doesn't understand it should read two messages.
this is mycode :
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
settings = context.getSharedPreferences("settings", context.MODE_PRIVATE);
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
if (msgs != null) {
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
}}}
How can I solve this
try this
Bundle bundle = intent.getExtras();
messages = (Object[]) bundle.get("pdus");
smsMessage = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
for (int i = 0; i < smsMessage.length; i++)
mainsms += smsMessage[i].getMessageBody();
And your complete SMS text is mainsms
I have built an SMS messaging app, which both sends and receives text messages. In MainActivity, I have a two-dimensional array of people's names and phone numbers, and in my sending class, I have a for loop which sends the same message to all of the recipients by going through each of the numbers:
for (i=0; i<names.length; i++) {
phoneNo = names[i][2] + names[i][3];
sendMessage(phoneNo, message);
}
private void sendMessage(String phoneNo, String message) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent", Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS failed. Please try again!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
When I send a message through the app, I can see very clearly from my own Samsung messaging app that the same message gets sent to each of the numbers in the list, which is perfect.
This is my shortened receiver class:
public class Receiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
SmsMessage[] smgs = null;
String infoSender = "";
String infoSMS = "";
if (extras != null) {
// Retrieve the sms message received
Object[] pdus = (Object[]) extras.get("pdus");
smgs = new SmsMessage[pdus.length];
for (int i = 0; i < smgs.length; i++) {
smgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
infoSender += smgs[i].getOriginatingAddress();
infoSMS += smgs[i].getMessageBody().toString();
}
}
I have found that despite the message being sent out once to each recipient, some recipients (with this app) receive it more than once consecutively. Hence, I suspected that there was something wrong with my receiver code, which is seemingly treating one received message as several consecutive received messages. This is not a consistent problem, as different people receive the consecutive messages at different times.
However, what I've also found is that if I hardcode phoneNo in the sending class to just one phone number, or if I have only one phone number in the array in MainActivity, then this problem doesn't occur. The message still gets sent out once to that one phone number only, but the receiver will always receive it just once as intended.
I am so confused by this now, so can somebody please give some suggestions as to what I could try? Literally in the last minute, I thought that it could be a problem with createFromPdu being deprecated? If so, please advise how to change my receiver code, as I couldn't find anything which resembles my current code too much.
Many thanks in advance:-)
Do like this you are making mistake check below code.
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());
}
senderNum = messages[0].getOriginatingAddress();
message = sb.toString();
}
Update: To check default app
public class Receiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
final String myPackageName = context.getPackageName();
if (Telephony.Sms.getDefaultSmsPackage(context).equals(
myPackageName)) {
// you are default
Bundle extras = intent.getExtras();
SmsMessage[] smgs = null;
String infoSender = "";
String infoSMS = "";
if (extras != null) {
// Retrieve the sms message received
Object[] pdus = (Object[]) extras.get("pdus");
smgs = new SmsMessage[pdus.length];
for (int i = 0; i < smgs.length; i++) {
smgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
infoSender += smgs[i].getOriginatingAddress();
infoSMS += smgs[i].getMessageBody().toString();
}
}
} else {
// you are not ignore
}
} else {
// for below KitKat do like normal
Bundle extras = intent.getExtras();
SmsMessage[] smgs = null;
String infoSender = "";
String infoSMS = "";
if (extras != null) {
// Retrieve the sms message received
Object[] pdus = (Object[]) extras.get("pdus");
smgs = new SmsMessage[pdus.length];
for (int i = 0; i < smgs.length; i++) {
smgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
infoSender += smgs[i].getOriginatingAddress();
infoSMS += smgs[i].getMessageBody().toString();
}
}
}
}
}
i hope this modication of your code base will help solve your problem
public class Receiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
SmsMessage[] smgs = null;
String infoSender = "";
String infoSMS = "";
if (extras != null) {
try{
// Retrieve the sms message received
Object[] pdus = (Object[]) extras.get("pdus");
if(pdus.length==0){return;}
smgs = new SmsMessage[pdus.length];
for (int i = 0; i < smgs.length; i++) {
smgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
infoSMS += smgs[i].getMessageBody();
}
infoSender = smgs[0].getOriginatingAddress();
}catch(Exception e){
e.printStackTrace ();
}
}
}
}
This is my code
public class SmsReceiver extends BroadcastReceiver {
private static final String PDUS = "pdus";
#Override
public void onReceive(Context context, Intent intent) {
Object[] pdus = (Object[]) bundle.get(PDUS);
return SmsMessage.createFromPdu((byte[]) pdus[0]);
}
}
Source text of sms:
even worse, mailparser seems to take a very simple single-part test email i have, but in addition to the 1.1, text/plain
Text that I get by calling methods message.getMessageBody() or getDisplayMessageBody():
even worse, mailparser seems to take a very simple single-part test
what am I doing wrong?
P.S. Hangouts returns full messages
Try this
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String sender = "", receivedMessage = "";
String MSG_TYPE = intent.getAction();
if (MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage
.createFromPdu((byte[]) pdus[i]);
sender = SMessage.getOriginatingAddress();
receivedMessage += SMessage.getMessageBody().toString();
}
}
}
SMS is typically composed of many pdus, so you need code like this:
retMsgs = new SmsMessage[pdus.length];
for(int n=0; n < pdus.length; n++) {
byte[] byteData = (byte[])pdus[n];
retMsgs[n] = SmsMessage.createFromPdu(byteData);
}
and then return the array.
I have a application which receive Data sms from Telecom. In my code, I registerd dynamic Broadcastreceiver within a Activity. It work fine, but sometime I lost a few sms message and I don't know why. This is my code
IntentFilter mFilter = new IntentFilter(
"android.intent.action.DATA_SMS_RECEIVED");
mFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
mFilter.addDataAuthority("localhost", "my port");
mFilter.addDataScheme("sms");
if (smsReceiver == null)
smsReceiver = new SmsReceiverBroadcast();
context.registerReceiver(smsReceiver, intentFilter);
after register BroadCast to activity I wait 60s
long time = 0;
int delay = 200;
while (!Variable.getMessage && !finish
&& !Util.isTimeout(time, Timer)) {
Util.sleep(delay);
time += delay;
}
And my BroadCastReceiver:
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null && bundle.containsKey("pdus")) {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
String pduString = toHexString((byte[]) pdus[0]);
LogParams.pdu = (byte[]) pdus[0];
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (msgs[i].getMessageBody() != null) {
str += msgs[i].getMessageBody().toString();
Variable.isReadPdu = true;
} else {
PduParser pduparser = new PduParser();
Pdu pdu = pduparser.parsePdu(pduString);
str += pdu.getDecodedText();
Variable.isReadPdu = true;
}
What's wrong with my code ?