• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

How to fetch details of the sent sms from an application in Android?

Requirement:
In Android With the help of my app i want to send sms to multiple recipients, also fetch details of message sent.
Details such as:
- Recipient Name
- Contact No
- Sent Date
- Message
- Status of the sent message (Delivered or not)

Issue facing:
Not able to fetch proper data of sent sms from BroadcastReceiver.

Ex:
I am sending sms with the help of SmsManager.
If i am sending sms to more than one recipients, and if sms sending failed/delivered to one of the recipient then i don't understand that exactly to which recipient it got failed/delivered. Because of which i am unable to update my database.

Here is my sample code:
Want result in place of
//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE// mention in code.

public class SendMessage
{

	static int _countSMS = 0;	
	static int _entryCount = 0;	
	static int _alarmId;
	static int _statusCounter=0;	
	static boolean _MessageStatus;
	static String MESSAGE_DELIVERY_NOTIFICATION;
	
	public void sendSMS(Context context,String phoneNumber, final String message) 
	{
		try
		{
			String SENT = "SMS_SENT";
			String DELIVERED = "SMS_DELIVERED";			
			
			PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
					new Intent(SENT), 0);

			PendingIntent deliveredPI = PendingIntent.getBroadcast(context,
					0, new Intent(DELIVERED), 0);

			// ---when the SMS has been sent---
			context.getApplicationContext().registerReceiver(new BroadcastReceiver() {

				@Override
				public void onReceive(Context context, Intent intent) {							
					switch (getResultCode()) {
					case Activity.RESULT_OK:																					
						_MessageStatus = false;
						
						[b]//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE//[/b]
						
						break;
					case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					
						[b]//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE//[/b]
					
						_MessageStatus = false;
						MESSAGE_DELIVERY_NOTIFICATION = " : Sending Failed.";						
						break;
					case SmsManager.RESULT_ERROR_NO_SERVICE:

						[b]//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE//	[/b]				
						
						_MessageStatus = false;
						MESSAGE_DELIVERY_NOTIFICATION = " : No Service Available.";						
						break;
					case SmsManager.RESULT_ERROR_NULL_PDU:		
                                        
[b]//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE//	[/b]	
							
						_MessageStatus = false;
						MESSAGE_DELIVERY_NOTIFICATION = " : Sending Failed.";						
						break;						
					}
 				}
			}, new IntentFilter(SENT));

			// ---when the SMS has been delivered---
			context.getApplicationContext().registerReceiver(new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {

					switch (getResultCode()) {					
					case Activity.RESULT_OK:	
					
						[b]//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE//[/b]
						
						_MessageStatus = true;
						MESSAGE_DELIVERY_NOTIFICATION = " : Delivered.";											
						break;
					case Activity.RESULT_CANCELED:		
						
						[b]//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE//[/b]
						
						_MessageStatus = false;
						MESSAGE_DELIVERY_NOTIFICATION = " : Not Delivered.";						
						break;					
					}
				}
			}, new IntentFilter(DELIVERED));

			SmsManager sms = SmsManager.getDefault();				
			String[] arrPhoneNos = phoneNumber.split(",");			
			for (int i = 0; i < arrPhoneNos.length; i++) {
				sms.sendTextMessage(arrPhoneNos[i].trim(), null, message, sentPI, deliveredPI);
				storeMessage(context,arrPhoneNos[i].trim(),message);				
			}								
		}
		catch (Exception e) 
		{
			e.toString();
		}
	}

	private static void storeMessage(Context context , String mobNo, String msg) 
	{ 
		try
		{
			ContentValues values = new ContentValues(); 
			values.put("address", mobNo); 
			values.put("body", msg); 
			context.getContentResolver().insert(Uri.parse("content://sms/sent"), values);	
		}
		catch (Exception e) 
		{
			e.toString();
		} 
	}
	
	
}

Open in new window

0
RTKHOT
Asked:
RTKHOT
1 Solution
 
SanthanaTechnical LeadCommented:
Hi RTKHOT,

While constructing Pending intent put PhoneNo as the Extra with the Intent.
After the broad cast get the intent extra, based on the result status update the Hashmaps
lDeliveryStatus, lSentStatus. By using this HashMap you can come to know the real status of the Msg whether it has SENT/DELIVERED successfully or not.

            Intent lIntent_Sent;
            Intent lIntent_Deliver;
            PendingIntent sentPI;
            PendingIntent deliveredPI ;
            HashMap<String,Boolean> lDeliveryStatus = new HashMap<String,Boolean>();
            HashMap<String,Boolean> lSentStatus = new HashMap<String,Boolean>();
            
            for (int i = 0; i < arrPhoneNos.length; i++) {

                lIntent_Sent = new Intent(SENT);
                lIntent_Deliver = new Intent(DELIVERED);
                lIntent_Sent.putExtra("SENT_NO", arrPhoneNos[i].trim());
                lIntent_Deliver.putExtra("DELIVER_NO", arrPhoneNos[i].trim());

                sentPI = PendingIntent.getBroadcast(context, 0, lIntent_Sent, 0);
                deliveredPI = PendingIntent.getBroadcast(context, 0, lIntent_Deliver, 0);

                sms.sendTextMessage(arrPhoneNos[i].trim(), null, message, sentPI, deliveredPI);

                storeMessage(context, arrPhoneNos[i].trim(), message);
            }       




context.getApplicationContext().registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {


                       case Activity.RESULT_OK:    
                    
                       //   [b]//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE//[/b]
                        String lDelNoOk =  intent.getStringExtra("DELIVER_NO");
                        if(!TextUtils.isEmpty(lDelNoOk)) {
                            
                            lDeliveryStatus.put(lDelNoOk, true);
                        }
                        _MessageStatus = true;
                        MESSAGE_DELIVERY_NOTIFICATION = " :Delivered.";                                            
                        break;
                    case Activity.RESULT_CANCELED:      
                        
//                        [b]//HOW TO FETCH DETAILS OF THE RECIPIENT DETAIL HERE//[/b]
                        String lDelNoCancell =  intent.getStringExtra("DELIVER_NO");
                        if(!TextUtils.isEmpty(lDelNoCancell)) {
                            
                            lDeliveryStatus.put(lDelNoCancell, false);
                        }  
}        

Open in new window


Regards
Santhana
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now