联系API问题与保存数据选项
我面临一个基本问题,但我没有找到任何教程 为了帮助我...
我正在编写一个带有备份联系人选项的应用程序。我想 我的应用程序适用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)