Contact API problem with save datas Options
I'm facing a basic problem but i didn't find any tutorial in order to help me...
I'm writing an application with sort of backup contact options. I want that my applications works for android phones since 1.5 to 2.2
So i write a two implementation of ContactApi, one for 1.5, 1.6 and an other for new api version.
Here is the list of problem I'm facing with.
With new api, nothing. All works fine, backing up contacts works well.
But with older api I'm not able to backing up some datas :
- Email Datas (able to read, but not able to save)
- IM datas (able to read, but not able to save)
- Notes (able to read the first note, if many notes, I lost datas, same things for backup)
Here is the code I'm using :
=======EMAIL=======
private ArrayList<Email> getEmailAddresses(String id) {
ArrayList<Email> emails = new ArrayList<Email>();
Cursor emailCur = this.contentResolver.query(Contacts.ContactMethods.CONTENT_EMAIL_URI, null, Contacts.ContactMethods.PERSON_ID + " = ?", new String[] { id }, null);
Email email = null;
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
email = new Email();
email.setData(emailCur.getString(emailCur.getColumnIndex(Contacts.ContactMe thods.DATA)));
email.setType(emailCur.getInt(emailCur.getColumnIndex(Contacts.ContactMetho ds.TYPE)));
email.setLabel(emailCur.getString(emailCur.getColumnIndex(Contacts.PeopleCo lumns.NAME)));
emails.add(email);
}
emailCur.close();
return emails;
}
private void saveEmailAddresses(ContentUris contactUri, List<Email> emailList, String id) {
if (emailList != null && emailList.size() > 0) {
ContentValues values = null;
ContentValues[] valueArray = new ContentValues[emailList.size()];
int i = 0;
for (Email email : emailList) {
values = new ContentValues();
values.put(Contacts.ContactMethods.PERSON_ID, id); //
values.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL); //
values.put(Contacts.ContactMethods.DATA, email.getData()); //
values.put(Contacts.ContactMethods.TYPE, email.getType()); //
values.put(Contacts.PeopleColumns.NAME, email.getLabel()); //
valueArray[i] = values;
i++;
}
contentResolver.bulkInsert(Contacts.ContactMethods.CONTENT_EMAIL_URI, valueArray);
}
}
======== IM adress=============
private ArrayList<IM> getIM(Cursor cur, String id) { ArrayList<IM> imList = new ArrayList<IM>(); String where = Contacts.ContactMethods.PERSON_ID + " = ? AND " + Contacts.ContactMethods.KIND + " = ?"; String[] whereParameters = new String[] { id, String.valueOf(Contacts.KIND_IM) }; Cursor imCur = this.contentResolver.query(Contacts.ContactMethods.CONTENT_URI, null, where, whereParameters, null); IM im = null; while (imCur.moveToNext()) { try { String imName = imCur.getString(imCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA)); im = new IM(); im.setName(imName); im.setType(imCur.getInt(imCur.getColumnIndex(Contacts.ContactMethodsColumns .TYPE))); im.setProtocol(cur.getString(imCur.getColumnIndex(Contacts.ContactMetho开发者_StackOverflowds.A UX_DATA))); imList.add(im); } catch (Exception e) { Log.e(where, "Error im : ", e); } } imCur.close(); return imList; } private void saveIM(List<IM> imList, String id) { if (imList != null && imList.size() > 0) { ContentValues values = null; ContentValues[] valueArray = new ContentValues[imList.size()]; int i = 0; for (IM im : imList) { values = new ContentValues(); values.put(Contacts.ContactMethods.PERSON_ID, id); // values.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM); // values.put(Contacts.ContactMethodsColumns.DATA, im.getName()); // values.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodeCustomImProtocol(im.getProtocol())); // values.put(Contacts.ContactMethodsColumns.TYPE, im.getType()); // valueArray[i] = values; i++; } contentResolver.bulkInsert(Contacts.ContactMethods.CONTENT_URI, valueArray); } }
==========Notes =======
I have no idea how to get all notes ?
Can someone help me with this ?
public static HashMap Parse(String XML, String Tag, ArrayList<String> NodesList)
{
HashMap v = new HashMap();
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(XML));
Document doc = db.parse(is);
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.getElementsByTagName(Tag);
nodeCount = nodeLst.getLength();
for (int s = 0; s < nodeCount; s++)
{
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE)
{
Element fstElmnt = (Element) fstNode;
for(String nodeName : NodesList)
{
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(nodeName);
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
try {
String NodeValue = ((Node) fstNm.item(0)).getNodeValue().toString().trim();
if(NodeValue !=null )
v.put(nodeName + s, NodeValue);
}
catch(Exception e) {
v.put(nodeName + s, "");
}
}
}
}
}
catch (Exception e)
{
//e.printStackTrace();
}
return v;
}
精彩评论