Wednesday, October 12, 2011

[android-developers] Contact lookup by phone does not work

Hi Folks
im trying to build an app that among other things reads the contact
information stored in my contacts based on the phone number received
in an sms.
This is the code im using for the search that is currently not working
for me

package SMS_Listener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
//import android.provider.Contacts;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;
import android.view.Gravity;
import android.widget.Toast;

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 = "";
String numero = "";
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();
numero += msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}

System.out.println("Su Numero es: " + numero);
//--Get the name of the contact from contact list based on
the phone number

//change numero to an int value
int numeroi = Integer.parseInt(numero);
//

// This is where the program fails i have commented out in my code to
make it work for the moment

Uri lookupUri =
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(numero));
/*if (lookupUri != null) {
Cursor c=context.getContentResolver().query(
lookupUri,new String[]
{PhoneLookup.DISPLAY_NAME},null,null,null);

while(c.moveToNext()){
String name=c.getString(0);
System.out.println("name="+name);
}
c.close();
}

else if (lookupUri == null){
System.out.println("No existe el numero en los
contactos");
}
*/
// end get contact info


//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}


All your help will be highly appreciated,
Thanks people,
rgv

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment