Android application with phone book synchronization?(与电话簿同步的 Android 应用程序?)
问题描述
我正在创建一个 android 测试应用程序,其中我有一个按钮.单击按钮时,我想将电话簿记录与我的本地数据库同步.如果电话簿中的记录不在 db 表中,则插入它,否则保持原样.那我该怎么做呢?
I am creating one android test application , in which i have one button. On button click ,i want to synchronize phonebook records with my local database.If record in phone book is not in db table then insert it , otherwise leave it as it is. so how can i do this?
推荐答案
要从电话簿中获取联系人列表,您需要在 AndroidManifest.XML
中写入权限(即 android.permission.READ_CONTACTS
) .您可以使用以下方法收集联系人列表.
For getting contact list from your phone book you need write permission in AndroidManifest.XML
(i.e. android.permission.READ_CONTACTS
) .And you can collect contact list using following method.
ShowContact()
{
ArrayList<String> nameList;
ArrayList<String> phoneNoList;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Query phone here. Covered next
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
nameList.add(name); // Here you can list of contact.
phoneNoList.add(phoneNo); // And here you can get list of phone number.You have to query separately for getting phone_no,email,name etc
// Here you have to iterate this(i.e. nameList) with your list in the database.And your rest of logic.
}
pCur.close();
}
}
}
}
如果在获取联系人列表时遇到任何问题,请告诉我.
And Let me know if are having any issue in getting contact list.
这篇关于与电话簿同步的 Android 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:与电话簿同步的 Android 应用程序?
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01