tags:

views:

4151

answers:

2

Hi, I'm having a problem reading the SMS messages from the device. When acquiring a content provider for the URI 'content://sms/inbox', everything is fine, I can read the "person" column to find the foreign key into the people table and ultimately reach the contact and their name.

However, I also want to traverse the sent messages too. When reading from 'content://sms/sent', the person field always appears to be 0. Is this the correct field to be reading to locate the recipient data for the sent message? If so - any idea why mine is always 0?

All my testing has been done in the emulator and I've created 3 contacts. I've sent messages to those contacts from the emulator in the normal manner you'd send a message.

Just to reiterate, I can see the 4 sent messages and read the associated body text. My problem is that I can't seem to read the "person" ID and hence I can't work out who the recipient is.

Any help would be greatly appreciated.

Many thanks,

Martin.

+5  A: 

Use the address column. I guess the person column is ignored because people can send SMSs to phone numbers that are not in the contacts list.

// address contains the phone number
Uri phoneUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address);
if (phoneUri != null) {
  Cursor phoneCursor = getContentResolver().query(phoneUri, new String[] {Phones._ID, Contacts.Phones.PERSON_ID}, null, null, null);
  if (phoneCursor.moveToFirst()) {
    long person = phonesCursor.getLong(1); // this is the person ID you need
  }
}
Tughi
A: 

hi martin,

Here i m attaching code which i have written to send msg to users that i select from phone book

addcontact.setOnClickListener(new View.OnClickListener() { public void onClick(View V) { Intent ContactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); startActivityForResult(ContactPickerIntent, CONTACT_PICKER_RESULT);
} } ); This will open contact list .............................. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT: Cursor cursor=null; try {
Uri result = data.getData(); Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());

                 // get the contact id from the Uri     
                 String id = result.getLastPathSegment();

                 // query for everything contact number  
                 cursor = getContentResolver().query(  
                      Phone.CONTENT_URI, null,  
                      Phone.CONTACT_ID + "=?",  
                      new String[]{id}, null); 

                 cursor.moveToFirst();
                 int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                 if (cursor.moveToFirst())
                 {   
                     phonenofromcontact = cursor.getString(phoneIdx);
                     finallistofnumberstosendmsg +=","+phonenofromcontact;
                     Log.v(DEBUG_TAG, "Got email: " + phonenofromcontact);  
                 }
                 else 
                 {                                
                     Log.w(DEBUG_TAG, "No results"); 
                 }
             }
             catch(Exception e)
             {
                 Log.e(DEBUG_TAG, "Failed to get contact number", e);
             }
             finally
             {
                 if (cursor != null)
                 {  
                     cursor.close();
                 }
             }
             phonePhoneno= (EditText)findViewById(R.id.Phonenofromcontact);
             phonePhoneno.setText(finallistofnumberstosendmsg);
             //phonePhoneno.setText(phonenofromcontact);
             if(phonenofromcontact.length()==0)
             {
                 Toast.makeText(this, "No contact number found for this contact",
                         Toast.LENGTH_LONG).show(); 
             }
            break;  
         }  
     } 
     else
     {  
         Log.w(DEBUG_TAG, "Warning: activity result not ok");
     }  
 }  

This is how u can handle and get the phone number from phonebook ..................................................................

Now call send msg with the list of number and msg to set..

private void sendSMS(String phoneNumber, String message) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);

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

  //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    },new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       
}

This will send message ................................... U need reciever to recieve message broadcasted

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //---get the SMS message passed in---
    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 += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }   
}

}

You can also try it out. It works for me.. Thanks

Rakesh Gondaliya