I'm using ContactsContract to insert RawContacts in my app. The following code (and showing the inserted contact in the contact app) works on all devices, but not on Sony Xperia (Android 4.4.4).
ContentValues p=new ContentValues();
p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, getActivity().getPackageName());
p.put(ContactsContract.RawContacts.ACCOUNT_NAME,
DataHelper.getAppName(getActivity()));
Uri rowcontact = null;
long rawcontactid = 0;
try {
rowcontact = getActivity().getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, p);
rawcontactid = ContentUris.parseId(rowcontact);
Log.d(DEBUG_KEY, "CONTACT ADDED: " + rawcontactid);
}catch(Exception e){
Log.d(DEBUG_KEY, "CONTACT ADDED FAILED 1: " + e.getMessage());
return "";
}
On the Sony Xperia Device there is no error or exception. The console display the correct CONTACT-ADDED-ID. But in the android contact app the newly inserted (Raw)-Contact is not showing. I have enabled all groups, etc. in the filter-settings in the contact app.
Ok. Problem is solved. On Sony Xperia Devices you must
(1) Specify an existing (google) account
p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");
p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "google_account_username_on_device");
or (2) don't specify any account type details
//p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");
//p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "google_account_username_on_device");
On other devices like Motorola oder Google Nexus you can specify a non existing, custom Account, like:
p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "my.app.name");
p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "APP NAME");
Related
Is there way to programmatically adds the contact to the internal phone contacts book as a "phone contact"?
I've tried:
list.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
with this parameters contact successfully saves to the phone, but in case i set up filter to "display phone contacts only" - created contact not appears. btw, i've read that contacts with null type can be loses drying accounts synchronization (haven't remember the full case)
then i tried to retrieve ACCOUNT_TYPE and ACCOUNT_NAME from existing phone contact and gets Phone and Local Phone Account strings, but when i tried to save contact with same parameters:
list.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "Phone")
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "Local Phone Account")
.build());
the result was the same to the first case with null type.
is there any constants (or it should be another way) to save data like "phone contact"?
The "phone only" account is not fully supported by plain Android, it's usually added (or unlocked) by device makers.
Here are the ones I know of, feel free to add more in case you find more.
The format is <maker>: ACCOUNT_TYPE, ACCOUNT_NAME
samsung: "vnd.sec.contact.phone: "vnd.sec.contact.phone"
htc: "com.htc.android.pcsc: "pcsc"
sony: "com.sonyericsson.localcontacts: "Phone contacts"
lge: "com.lge.sync: "Phone"
lge (option 2): "com.lge.phone"
t-mobile: "vnd.tmobileus.contact.phone: "MobileLife Contacts"
huawei: "com.android.huawei.phone: "Phone"
lenovo: "Local Phone Account: "Phone"
xiaomi: "com.xiaomi"
oppo: "com.oppo.contacts.device"
Looking for some help regarding querying in different verions of Android. I have the following code that returns a cursor of bookmarks. I am trying to filter the browser to return only urls that are actual bookmarks, not just browser history. It works on version 3.1 but on my new Nexus 7, it will not filter by bookmark, but instead returns all browser history in the cursor. Any insight is much appreciated. I think I have run into issues with filtering and content resolver query's not paying attention to selection parameters but can't seem to find any info. Thanks.
String[] mColumnStrings =
{
Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL,
Browser.BookmarkColumns._ID,
Browser.BookmarkColumns.BOOKMARK
};
try{
bookmarksCursor = getActivity().getContentResolver().query(Browser.BOOKMARKS_URI, mColumnStrings, Browser.BookmarkColumns.BOOKMARK+ " = 1 ", null , Browser.BookmarkColumns.URL + " ASC");
getActivity().startManagingCursor(bookmarksCursor);
return bookmarksCursor;
Its working fine for me...I tested it on AVD...actually Android 4.1 already have some default bookmarks...print the Browser.BookmarkColumns.BOOKMARK as well to verify weather the results are bookmarked or not...
You can use this method specifically for your to varify...
private void varify(Cursor bookmarksCursor) {
bookmarksCursor.moveToFirst();
while(bookmarksCursor.moveToNext()) {
Log.v("title", bookmarksCursor.getString(0));
Log.v("url", bookmarksCursor.getString(1));
Log.v("id", bookmarksCursor.getString(2));
Log.v("bookmark", bookmarksCursor.getString(3));
}
}
Hope this works...
If you decide that this answers your question, please mark it as "accepted". This will raise both your and my reputation score.
I am developing app which add contact info to android contact list .to How to add postal address to contacts in android programmatically ?
It's been a while that this has been asked but maybe someone else is still interested in it. How to add a contact with address info:
import static android.provider.ContactsContract.Data;
import static android.provider.ContactsContract.Intents.Insert;
private void createContactIntent() {
Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION, ContactsContract.Contacts.CONTENT_URI);
contactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
contactIntent.putExtra(Insert.NAME, "Sergio Mendes");
contactIntent.putExtra(Insert.COMPANY, "Company One");
contactIntent.putExtra(Insert.POSTAL, "Street 1, 9999 City, Country");
contactIntent.putExtra(Data.IS_SUPER_PRIMARY, 1);
startActivity(contactIntent);
}
Note that some devices like Samsung S5 / A5 will put the whole address into the "street" field. If you have any optimizations for this let me know.
Postal address are stored like all the other info in the DATA table with a
MIMEtype == ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
Please google ContactsContract.CommonDataKinds.StructuredPostalto find all the info.
If you need to know how to edit a contact in general I would suggest you to have a look to the SampleSyncAdapter in the Android SDK. It is a sync adapter so you don't need to study everything but updateContact in ContactManager is a good point to start with.
I am using update method to update my contact but when i go to my contact list to see the udpated number it shows me the previous number store , whats wrong with code can any sort it , how to udpate the contacts
ContentValues newPhone = new ContentValues();
updateUri=Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
newPhone.put(People.Phones.TYPE, People.TYPE_MOBILE);
newPhone.put(People.NUMBER, phone);
//newPhone.clear();
Log.v("--- PHONE NUMBER ","---VALUE ---- "+phone);
//getContentResolver().update(updateUri, newPhone, null,null);
getContentResolver().insert(updateUri, newPhone);
Toast.makeText(NativeContentProvider.this, "Updated the phone number to: " + phone, Toast.LENGTH_SHORT).show();
Log.i(getClass().getSimpleName(), "Updated the phone number");
add permission to add/edit contact in menifest .
if already done , tell more about error you are facing . error log result will be very helpful to understand
I am developing app which add contact info to android contact list .to How to add postal address to contacts in android programmatically ?
It's been a while that this has been asked but maybe someone else is still interested in it. How to add a contact with address info:
import static android.provider.ContactsContract.Data;
import static android.provider.ContactsContract.Intents.Insert;
private void createContactIntent() {
Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION, ContactsContract.Contacts.CONTENT_URI);
contactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
contactIntent.putExtra(Insert.NAME, "Sergio Mendes");
contactIntent.putExtra(Insert.COMPANY, "Company One");
contactIntent.putExtra(Insert.POSTAL, "Street 1, 9999 City, Country");
contactIntent.putExtra(Data.IS_SUPER_PRIMARY, 1);
startActivity(contactIntent);
}
Note that some devices like Samsung S5 / A5 will put the whole address into the "street" field. If you have any optimizations for this let me know.
Postal address are stored like all the other info in the DATA table with a
MIMEtype == ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
Please google ContactsContract.CommonDataKinds.StructuredPostalto find all the info.
If you need to know how to edit a contact in general I would suggest you to have a look to the SampleSyncAdapter in the Android SDK. It is a sync adapter so you don't need to study everything but updateContact in ContactManager is a good point to start with.