I wrote an sms sending application using the instructions Here. Now I want to add a suggestion for the phone number when the number or name is typed according to the number saved in the phone book i.e like when we enter number to call it suggests a number(dynamically on typing).
Is it possible to do or not? If possible then How Can I do this?I have used API level 2.2.
Edit:
I have do this with the help of Question Here But When I click on the name from phone-book,it place name on text-box but I have need the number to send sms.
Question: How could I get number so that I can sent message to the selected name.
Yes, it is possible. You can change fetch the existing numbers in your phone with ContactsContract, change your phone number EditText to an AutoCompleteTextView, and use a SimpleCursorAdapter to bind the numbers from your contacts to the AutoCompleteTextView.
Addition
If you want to convert a contact name into a phone number you can try this in your "Submit" Button's onClick() method:
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.Contacts.DISPLAY_NAME + " = '" + autoComplete.getText() + "'",
null, null);
if(cursor.moveToFirst())
phoneNo = cursor.getString(0);
Understand that this only gets the first number from the first contact that matches the name from your AutoCompleteTextView. You'll have to discern a way to distinguish between contacts with the same name and contact with multiple numbers.
Related
I have a contact in my contact list and her name is start with ♥ character, this character code is 9825. Every dialer & contacts application show this item on the first place, but if I get the list in my app with this order:
ContactsContract.Data.DISPLAY_NAME + " ASC"
then this contact will be the last. I also tried sort by Collections class but same result.
My question is that the other application how order the list that the it starts with the special character's item?
Thank you!
i did a lot of research but couldn't find anything to help;
my problem is: i need to create an application where the user can select some of the contacts on his phone to be added to this application where he/she can later communicate with them via sms in a special template. but the user need to select only one phone number to be active to this user on this application. this choice must be caved for later logons.
i was able to retrieve contacts and their phone numbers using lookupkey (which will be saved in my application as a reference for preselected users), but i couldn't figure out how to tag the needed phone number, i was thinking of adding a flag to the phone number but i dont know how, i dont know if this is the right way to do it, i thought of setting the selected phone number as primary then query t when needed... or simply save the phone number id (but i am not sure if saving the id is safe in case user changed the phone numer)...
thx for any help...
After a long period of trial and error I found the solution to my problem. I will be using the contacts lookup key to store the contact and the phone id to store the phone number...as follows:
String selection = ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY + " = '" + lookupkey+ "' and "+Phone._ID+"='"+phoneid+"'";
String[] projection =new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER};
Cursor managedCursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, Phone.DISPLAY_NAME + " ASC");
I'm a bit new to all this stuff. As I understand it, an id match is fast, but may not be stable. If a key+id match fails, you should also try a slower match using only the key - Jeffrey Scofield's SQL selection string could be changed to try id-and-key OR key-only (trusting the query optimiser to prioritize the id match).
I haven't had much luck finding information concerning the wisdom of storing the key long term.
I am currently using
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://sms/");
Cursor messagesCursor = cr.query(uri, new String[] { "_id", "address", "body", "person" }, null, null, null);
if (messagesCursor.getCount() > 0) {
while (messagesCursor.moveToNext()) {
colName = getContactName(messagesCursor.getString(messagesCursor.getColumnIndex("address"))) + "\n";
}
}
to get all the text in the user's inbox. From there, messagesCursor.getColumnIndex("address") will get the SMS's number. Then it will be place in getContactName() where it will then get the contacts' name from their contact's list.
This method will take a long time as it goes though EVERY SINGLE text message, get number, then get the contact's name, check if name exist in array, if it doesn't put in array then from there create a ListView.
The app will take a long time to load on start up if the user's inbox contains lots of text.
Is there any special way to get UNIQUE numbers from inbox? Something in cr.query?
Thanks in advance.(:
EDIT :
I'm creating just a simple inbox application. It reads from the original inbox message and list the messages in it.
For this project, what I did was to read through ALL the "address" which is the phone number, put it in an ArrayList, and finally to the ListView. As it cycles through all the messages in inbox, it takes a long time to startup.
For example :
Inbox Messages
Person 1
Person 1
Person 1
Person 2
Person 2
Person 3
Person 3
Person 3
Person 3
What my current code does :
Read EVERY SINGLE message
Check if name in ArrayList
If it is, skip. If not, add in array.
Throw ArrayList into ListView.
What my intention was :
A code that only reads out :
Person 1
Person 2
Person 3
Try only using "address" in projection (that means, don't include even "_id") - I'm not sure about this but it may solve the case.
I am working on an Android Contact application and I would like to implement contact lookup using one particular keyword.
This keyword can be either the contact name, phone, email, and so on. Is this possible?
And if so, could anyone please point me in the right direction.
Thanks.
[RE-EDIT]
I was wondering is it okay to use this to lookup the keyword?
Cursor cur = this.context.getContentResolver().query(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
null,
ContactsContract.PhoneLookup.LOOKUP_KEY + " like '" + keyword + "%'",
null,
null);
Please use this you can pick up from the phone like phone number and contact name.
ContactsContract.Data.DISPLAY_NAME
ContactsContract.Data.DATA1
I am trying to search for a particular contact number in call log and in contact book of the android phone.
But I found that the contact number are stored in different way in contact book and call log. For e.g. I have a contact number 9889880912 to search in the call log and contact book.
To do this, I do the following:
add a contact in the android emulator. When i write the contact number for the contact as 9889880912 , it automaticlly gets changed to 988-988-0912 The dash sign (-) are authomatically inserted in the contact number. I saved the number.
Make a call to this number from the emulator.
Now, using my code,
when I am searching for a contact number in contact list by 9889880912,it shows no result
when I am trying to search by contact number 988-988-0912 , the contact information from contact book is displayed.
However, when I am trying to search for 988-988-0912 in call log history, it is displayed. but when I am trying to search for 9889880912 in call history , I get no result.
Please help me in solving my problem. Ho can I get the contact search by 9889880912 in contact book.
MY code :
/** Search from contact book **/
Cursor phoneResult = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI ,
columnNames,
queryString,
new String[] {String.valueOf(phoneNumber)},
null);
if(phoneResult.getCount() > 0)
Log.d("DEBUG","at least one contact with phonenumber");
/** Search from call log **/
Cursor callLogResult = context.getContentResolver().query(
callLogURI,
columnNames,
Calls.NUMBER + "=?",
new String[]{ String.valueOf(mobileNumber) },
null);
if(callLogResult.getCount() > 0)
Log.d("DEBUG","At least one call Log found");
Please help me in solving my problem. I am stuck with this since a long time and cant find solution.
What API level do u use? Any restrictions on that?
If 5 or higher you can try using PhoneLookup, see for example: http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html