I'm displaying a list of Contacts, and have a context menu to Edit Contact by calling an intent. On some contacts it works fine, but on others the Edit Contact activity is blank. Any ideas?
Here is the cursor...
projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID};
uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
cursor = getActivity().getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
Here is the code from my CursorAdapter.getView() ...
textView.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) ;
And here is the code from my onContextItemSelected...
cursor.moveToPosition(position);
String idContact = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Intent i = new Intent(Intent.ACTION_EDIT);
i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + idContact));
parent.startActivity(i);
I've checked logcat and can see
I/ActivityManager( 102): Starting activity: Intent { act=android.intent.action.EDIT dat=content://com.android.contacts/contacts/lookup/23356 cmp=com.android.htccontacts/.ui.EditContactActivity }
but no error messges
Try this:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = this.getContentResolver().query(uri, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
long idContact = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
then
Intent i = new Intent(Intent.ACTION_EDIT);
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, idContact);
i.setData(contactUri);
Related
I use the following code to add a birthday to the contact.
String contactName="";
String contactId="";
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(mobileItemEdit.getText().toString()));
String[] projection = new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};
Cursor cursor =
contentResolver.query(
uri,
projection,
null,
null,
null);
if(cursor!=null) {
while(cursor.moveToNext()){
contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
Log.d("LOGTAG", "contactMatch name: " + contactName);
Log.d("LOGTAG", "contactMatch id: " + contactId);
}
cursor.close();
}
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
values.put(ContactsContract.CommonDataKinds.Event.START_DATE, birthdayItemEdit.getText().toString());
Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
Inserting the event to the contact works, however, the google calendar does not show up the birthday event.
How to let google calendar show the contact's birthday? any help? thanks!
Given a contact name, I need to change its ringtone to the default ringtone,
Presently what I am doing is
1) Iterate through all the contacts
2) If the desired contact is found, prepare a contentvalues with the new ringtone(xperia.mp3 which is present in the Internal Storage.) and update it.
But the code is showing no effect ?
I am new to android and I need help in doing this, also read other answers, but they were not of great help.
String whereName = ContactsContract.Data.MIMETYPE + " = ?";
String[] whereNameParams = new String[]{ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
// Get cursor to all the names
Cursor nameCur = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
// if name equals name to check
if(display.equals(tocheck)){ // to check contains the name to check.
Uri contactData = ContactsContract.Contacts.CONTENT_URI;
String contactId = nameCur.getString(nameCur.getColumnIndexOrThrow("_id"));
String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
Cursor localCursor = getActivity().managedQuery(contactData, PROJECTION, null, null, null);
localCursor.move(Integer.valueOf(contactId)/*CONTACT ID NUMBER*/);
String str1 = localCursor.getString(localCursor.getColumnIndexOrThrow("_id"));
String str2 = localCursor.getString(localCursor.getColumnIndexOrThrow("display_name"));
Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, str1);
ContentValues localContentValues = new ContentValues();
File newSoundFile = new File(Environment.getExternalStorageDirectory() + File.separator + "xperia.mp3"); // save new ringtone.
localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE,newSoundFile.getAbsolutePath());
getActivity().getContentResolver().update(localUri, localContentValues, null, null);
nameCur.close();
break;
}
}
I am trying to create a Custom Contact app which displays only those contacts that have Contact Number. First of all, is there any automated way to do it? Suppose not, then I am trying to search a contact by its name e.g. Rohan.
Here is the code :-
Cursor photoCursor = getContentResolver().query(
android.provider.ContactsContract.Contacts.CONTENT_URI,
new String[] { ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.DISPLAY_NAME },
ContactsContract.Contacts.DISPLAY_NAME + " = ?",
new String[]{"Rohan"}, null);
photoCursor.moveToFirst();
while (photoCursor.moveToNext()) {
Log.d("Photo Thumbnail", "" + photoCursor.getString(1));
}
Although the contact exists, I am not getting any Log, if I remove Selection & Selection Args I see Rohan in the log. What am I doing wrong?
Simple Solution for Searching Partial Display Name.
ContentResolver contentResolver = getCurrentActivity().getContentResolver();
String whereString = "display_name LIKE ?";
String[] whereParams = new String[]{ "%" + searchText + "%" };
Cursor contactCursor = contentResolver.query(
ContactsContract.Data.CONTENT_URI,
null,
whereString,
whereParams,
null );
while( contactCursor.moveToNext() ) {
int contactId = getIntFromCursor( contactCursor, ContactsContract.Data.CONTACT_ID );
Log.d( "Contact ID", contactId)
}
contactCursor.close();
I did it by using the following code
Cursor cursor = getContentResolver().query(
android.provider.ContactsContract.Contacts.CONTENT_URI,
new String[] { ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID },
ContactsContract.Contacts.HAS_PHONE_NUMBER, null,
ContactsContract.Contacts.DISPLAY_NAME);
This cursor gives all the contacts that have any phone number and then i save the unique ID in an ArrayList like this
cursor.moveToFirst();
while (cursor.moveToNext()) {
contactsID.add(cursor.getString(2));
}
then on selecting the contact i find the contact numbers using this
Cursor cursor = getContentResolver()
.query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { contactsID.get(position) }, null);
contactNumbers = new ArrayList<String>();
while (cursor.moveToNext()) {
contactNumbers.add(cursor.getString(0));
Log.d("number", cursor.getString(0));
}
Try this:
Cursor contactLookupCursor =
getContentResolver().query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode("Rohan")),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER},
null,
null,
null);
try {
while (contactLookupCursor.moveToNext()) {
contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactNumber = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.NUMBER));
}
} finally {
contactLookupCursor.close();
}
It looks like you are trying to implement a screen that will allow the user to select a contact, and then select a phone number of that contact.
If that's the case, you can use a phone-picker intent instead:
Intent intent = Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
This will open the native Contacts app, and allow the user to select a contact, and a phone number.
You'll then receive the result in your app like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
...
}
}
}
I need to call recent call list and favourite call list on the click of respective buttons and need the data in my own list layout.I am new to android and having lot of trouble with this.can anyone please help me..thanks in advance..
With some extra, useful code:
getFavoriteContacts:
Map getFavoriteContacts(){
Map contactMap = new HashMap();
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = getContentResolver().query(queryUri, projection, selection,null,null);
while (cursor.moveToNext()) {
String contactID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
String intentUriString = intent.toUri(0);
String title = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
getRecentContacts:
Map getRecentContacts(){
Map contactMap = new HashMap();
Uri queryUri = android.provider.CallLog.Calls.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.DATE};
String sortOrder = String.format("%s limit 500 ", CallLog.Calls.DATE + " DESC");
Cursor cursor = getContentResolver().query(queryUri, projection, null,null,sortOrder);
while (cursor.moveToNext()) {
String phoneNumber = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.NUMBER));
String title = (cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
if(phoneNumber==null||title==null)continue;
String uri = "tel:" + phoneNumber ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
String intentUriString = intent.toUri(0);
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
For getting recent calls list,you can use CallLog in android. Here is a good tutorial.This is also helpful.
You can use it for all outgoing calls like this :
Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, android.provider.CallLog.Calls.TYPE+"="+android.provider.CallLog.Calls.OUTGOING_TYPE, null,null);
For all types of calls,use it like:
Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, null, null,null);
You can use the following Java code to get the recent calls list:
Cursor phones = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null,null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(CallLog.Calls.NUMBER));
if (phoneNumber.length() > 6) {
String name = phones.getString(phones.getColumnIndex(CallLog.Calls.CACHED_NAME));
numbers.add(phoneNumber);
}
}
I am developing the android application ,when ever user clicks on the button it should show all contacts from the phone book with in a table.How can i achieve it,any one can help me.thanks in advance
If you query the ContactsContract.Contacts content provider you will get cursor with the list of contacts.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
Use this piece of code under the button.setOnClick function you'll get the display of all the contacts in the phone book
give you some codes:
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()){
//get name
int nameFiledColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFiledColumnIndex);
String[] PHONES_PROJECTION = new String[] { "_id","display_name","data1","data3"};//
String contactId = cursor.getString(cursor.getColumnIndex(PhoneLookup._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PHONES_PROJECTION,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
//name type ..
while(phone.moveToNext()) {
int i = phone.getInt(0);
String str = phone.getString(1);
str = phone.getString(2);
str = phone.getString(3);
}
phone.close();
//addr
Cursor addrCur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI ,
new String[]{"_id","data1","data2","data3"}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId , null, null);
while(addrCur.moveToNext()) {
int i = addrCur.getInt(0);
String str = addrCur.getString(1);
str = addrCur.getString(2);
str = addrCur.getString(3);
}
addrCur.close();
//email
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI ,
new String[]{"_id","data1","data2","data3"}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId , null, null);
while(emailCur.moveToNext()) {
int i = emailCur.getInt(0);
String str = emailCur.getString(1);
str = emailCur.getString(2);
str = emailCur.getString(3);
}
emailCur.close();
}
cursor.close();
You can use this code inside the button.setonclicklistener.
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);