联系API问题与保存数据选项

发布于 2024-10-05 05:29:10 字数 5285 浏览 2 评论 0原文

我面临一个基本问题,但我没有找到任何教程 为了帮助我...

我正在编写一个带有备份联系人选项的应用程序。我想 我的应用程序适用于 Android 手机从 1.5 到 2.2

所以我编写了 ContactApi 的两个实现,一个用于 1.5、1.6,另一个用于 其他用于新的 api 版本。

这是我面临的问题列表。

使用新的 api,什么都没有。一切正常,备份联系人效果很好。

但使用较旧的 API,我无法备份某些数据:

  • 电子邮件数据(能够读取,但无法保存)
  • IM 数据(能够读取,但无法保存)
  • 注释(能够读取第一个注释,如果笔记很多,我会丢失数据, 备份的内容相同)

这是我正在使用的代码:

=======电子邮件=======

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 地址==============

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.ContactMethods.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); 
                } 
        } 

====== ====笔记=======

我不知道如何获取所有笔记?

有人可以帮我吗?

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.ContactMethods.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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2024-10-12 05:29:10
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文