Android documentation for SMSManagers sendTextMessage function
public void sendTextMessage (String destinationAddress, String scAddress, String text,
PendingIntent sentIntent, PendingIntent deliveryIntent)
deliveryIntent if not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu")
I could not understand if deliveryIntent is fired when SMS is delivered to destinationAddress or scAddress and what is the meaning of "raw pdu of the status report is in the extended data ("pdu")" and how to get that report? .
I appreciate your effort.
It is broadcast when message is delivered to destinationAddress.
The PDU may be extracted from the Intent.getExtras().get("pdu") when registered BroadcastReceiver receives the Intent broadcast you define with PendingIntent.getBroadcast(Context, int requestCode, Intent, int flags). For example:
private void sendSMS(String phoneNumber, String message) {
String DELIVERED = "DELIVERED";
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
registerReceiver(
new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
Object pdu = arg1.getExtras().get("pdu");
... // Do something with pdu
}
},
new IntentFilter(DELIVERED));
SmsManager smsMngr = SmsManager.getDefault();
smsMngr.sendTextMessage(phoneNumber, null, message, null, deliveredPI);
}
Then you need to parse extracted PDU, SMSLib should be able to do that.
Just to build on a.ch's answer, heres how you can extract the delivery report from an intent:
public static final SmsMessage[] getMessagesFromIntent(Intent intent) {
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
if (messages == null || messages.length == 0) {
return null;
}
byte[][] pduObjs = new byte[messages.length][];
for (int i = 0, len = messages.length; i < len; i++) {
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
SmsMessage[] msgs = new SmsMessage[pdus.length];
for (int i = 0, count = pdus.length; i < count; i++) {
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}
Full credit to the great project at: http://code.google.com/p/android-smspopup/
Related
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 ();
}
}
}
}
I am trying to implement Broadcast receiver to implement notification pop-up upon SMS received on my Android device. However I have set a condition to detect only specific SMSs from pre-defined senders and then pop-up a notification. Here is what I have coded till now :
public class MyBroadcastReceiver extends BroadcastReceiver
{
private static final String TAG = "MyBroadCastReceiver";
String str = "";
static Context context;
String sender;
#Override
public void onReceive(Context arg0, Intent arg1)
{
// Log.i(TAG,"OnReceive ++ ");
Bundle bndl = arg1.getExtras();
SmsMessage[] msg = null;
if (null != bndl)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bndl.get("pdus");
msg = new SmsMessage[pdus.length];
if(msg[0].getOriginatingAddress().endsWith("AIRMTA") ||
msg[0].getOriginatingAddress().endsWith("ICICIB") ||
msg[0].getOriginatingAddress().endsWith("FCHRGE") ||
msg[0].getOriginatingAddress().endsWith("MYAMEX") ||
msg[0].getOriginatingAddress().endsWith("MOBIKW") ||
msg[0].getOriginatingAddress().endsWith("OLACAB") ||
msg[0].getOriginatingAddress().endsWith("HDFCB") ||
msg[0].getOriginatingAddress().endsWith("AIRMNY")
)
{
for (int i=0; i<msg.length; i++)
{
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
// str += "SMS From " + msg[i].getOriginatingAddress();
sender = msg[i].getOriginatingAddress();
str += " :\r\n";
str += msg[i].getMessageBody().toString();
str += "\n";
context = arg0;
}
//---display incoming SMS as a Android Toast---
// Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
//---Create a status bar notification for incoming sms-->
int mNotificationId = 001;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle(sender);
mBuilder.setContentText(str);
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.notification);
Intent resultIntent = new Intent(arg0, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(arg0, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
MapsFragment obj = new MapsFragment();
obj.initilizeMap();
}
}
}
}
Unfortunately the application is crashing every time I receive an SMS, be it from any sender. I am not able to understand what is going wrong with the code !
Can anyone help me out on this ?
Here is the log cat error report :
Process: com.techfrk.fetchinboxsms, PID: 21956
java.lang.RuntimeException: Unable to start receiver com.techfrk.fetchinboxsms.MyBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.telephony.SmsMessage.getOriginatingAddress()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2616)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
You get a NullPointerException exception because your msg array is always empty when calling msg[0].getOriginatingAddress().endsWith().
With msg = new SmsMessage[pdus.length] you create an empty array with the size of pdus.length but this does not add any objects to it.
Instead of
if(msg[0].getOriginatingAddress().endsWith("AIRMTA") ||
msg[0].getOriginatingAddress().endsWith("ICICIB") ||
msg[0].getOriginatingAddress().endsWith("FCHRGE") ||
msg[0].getOriginatingAddress().endsWith("MYAMEX") ||
msg[0].getOriginatingAddress().endsWith("MOBIKW") ||
msg[0].getOriginatingAddress().endsWith("OLACAB") ||
msg[0].getOriginatingAddress().endsWith("HDFCB") ||
msg[0].getOriginatingAddress().endsWith("AIRMNY")
)
{
for (int i=0; i<msg.length; i++)
{
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
// str += "SMS From " + msg[i].getOriginatingAddress();
sender = msg[i].getOriginatingAddress();
str += " :\r\n";
str += msg[i].getMessageBody().toString();
str += "\n";
context = arg0;
}
}
you should do something like
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(arg1);
if(messages.length < 1) return;
SmsMessage sms = messages[0];
sender = sms.getOriginatingAddress();
str = sms.getMessageBody().toString();
if (sms.getOriginatingAddress().endsWith("AIRMTA")) { // add all your needed statements
// show your notification
}
Please keep in mind that this code is extremely simplified for a better understanding.
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've create a broadcast receiver that listen to the android.provider.Telephony.SMS_RECEIVED event and creates its own notification.
I also use the same receiver in the app to update the activities when an sms is received using a callback.
The problem is that the sms app notification event is dispatched after my notification, so when I update the sms is not preset in content://sms
I would like to delay my notification if it's possible, can't find how to do it.
here's the code:
public class SmsReceiver extends BroadcastReceiver {
Context context;
int nmessages = 0;
#Override
public void onReceive(Context context, Intent intent) {
//—get the SMS message passed in—
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messages = "";
this.context = context;
if (bundle != null)
{
//—retrieve the SMS message received—
Object[] smsExtra = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[smsExtra.length];
nmessages = SmsHolder.getNumberUnreadSms(context) +1;
for (int i=0; i<msgs.length; i++)
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
//take out content from sms
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
putSmsToDatabase(sms, context );
}
//—display the new SMS message—
createNotification(SmsMessage.createFromPdu((byte[])smsExtra[0]), context);
updateActivity();
}
}
public void updateActivity(){
}
private void putSmsToDatabase( SmsMessage sms, Context context )
{
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
// Create SMS row
ContentValues values = new ContentValues();
values.put("address", sms.getOriginatingAddress().toString() );
values.put("date", mydate);
values.put("body", sms.getMessageBody().toString());
// values.put( READ, MESSAGE_IS_NOT_READ );
// values.put( STATUS, sms.getStatus() );
// values.put( TYPE, MESSAGE_TYPE_INBOX );
// values.put( SEEN, MESSAGE_IS_NOT_SEEN );
}
private void createNotification(SmsMessage sms, Context context){
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String contentTitle;
if (nmessages < 2){
contentTitle = "SMS: " + ContactsInterface.getContactDisplayNameByNumber(sms.getOriginatingAddress(), context);
}else {
contentTitle = nmessages + " " + context.getResources().getString(R.string.new_messages);
}
// construct the Notification object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(sms.getMessageBody())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(getIconBitmap())
.setNumber(nmessages);
builder.setAutoCancel(true);
//(R.drawable.stat_sample, tickerText,
// System.currentTimeMillis());
// Set the info for the views that show in the notification panel.
//notif.setLatestEventInfo(this, from, message, contentIntent);
/*
// On tablets, the ticker shows the sender, the first line of the message,
// the photo of the person and the app icon. For our sample, we just show
// the same icon twice. If there is no sender, just pass an array of 1 Bitmap.
notif.tickerTitle = from;
notif.tickerSubtitle = message;
notif.tickerIcons = new Bitmap[2];
notif.tickerIcons[0] = getIconBitmap();;
notif.tickerIcons[1] = getIconBitmap();;
*/
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, Login.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// Ritardo in millisecondi
builder.setContentIntent(resultPendingIntent);
// Note that we use R.layout.incoming_message_panel as the ID for
// the notification. It could be any integer you want, but we use
// the convention of using a resource id for a string related to
// the notification. It will always be a unique number within your
// application.
nm.notify(R.drawable.ic_drawer, builder.build());
}
private Bitmap getIconBitmap() {
BitmapFactory f = new BitmapFactory();
return f.decodeResource(context.getResources(), R.drawable.ic_sms);
}
}
If you just need to do it for a specific amount of time, probably the easiest way is to use a handler and use a postDelayed(). Something like this:
// SLEEP 5 SECONDS HERE ...
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// createNotification(SmsMessage.createFromPdu((byte[])smsExtra[0]), context);
updateActivity();
}
}, 5000);
If you want to wait for another action, that's a bit more complicated.
Is it possible to extract the user data header from an sms message? if yes, any ideas on how to proceed with it?
also, is it possible to make changes to the data header before sending an sms?
the source code for receive a sms goes:
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
Log.i(LOG_TAG, "onReceive");
if (extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
String fromAddress = message.getOriginatingAddress();
String messageBody = message.getMessageBody().toString();
Log.i(LOG_TAG, "From: " + fromAddress + " message: " + messageBody);
addNotification(context, fromAddress, messageBody);
}
}
so,it was byte[] type, so if you have a good knowledge of UDH which is described in User Data Header, I think it is possible.