我想获取保存在手机中的手机通讯录,SIM卡通讯录和Google通讯录,但我只收到手机和SIM卡联系人。如果我可以离线访问Google联系人,则必须将其存储在本地数据库的某个位置。
如何导入所有这些内容?我到处搜索但无法找到解决方案。
pDialog = new ProgressDialog(this);
pDialog.setMessage("Reading contacts...");
pDialog.setCancelable(false);
pDialog.show();
//fetching contacts from phone
contacts_fetched = false;
contacts_refreshed=false;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
//phoneContactName.add(name);
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)).trim();
//getting present country code of mobile
GetCountryZipCode(phoneNo);
if (CountryZipCode != null) {
if (phoneNo.contains("+")) {
phoneContactNos.add(phoneNo);
//Toast.makeText(this, phoneNo, Toast.LENGTH_SHORT).show();
} else {
//adding country code if not present
phoneNo = CountryZipCode + " " + phoneNo;
phoneContactNos.add(phoneNo);
//Toast.makeText(this, phoneNo, Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
phoneContactName.add(name);
}
}
pCur.close();
}
}
}
if (cur != null) {
cur.close();
}
//importing contacts from sim
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);
while (cursorSim.moveToNext())
{
simContactName=cursorSim.getString(cursorSim.getColumnIndex("name"));
simContactNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
simContactNo.replaceAll("\\D","");
simContactNo.replaceAll("&", "");
simContactName=simContactName.replace("|","");
System.out.println("SimContacts"+simContactName);
System.out.println("SimContactsNo"+simContactNo);
if(!phoneContactNos.contains(simContactNo))
{
GetCountryZipCode(simContactNo);
if (CountryZipCode != null) {
if (simContactNo.contains("+")) {
phoneContactNos.add(simContactNo);
phoneContactName.add(simContactName);
//Toast.makeText(this, phoneNo, Toast.LENGTH_SHORT).show();
} else {
//adding country code if not present
simContactNo = CountryZipCode + " " + simContactNo;
phoneContactNos.add(simContactNo);
//Toast.makeText(this, phoneNo, Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "Sim Contact Name: " + simContactName);
Log.i(TAG, "Sim Phone Number: " + simContactNo);
}
}
}
答案 0 :(得分:0)
People API同时也是建议的方法 - 与之前的Contacts API相比:
对用户的读写访问权限'联系人,使用People API,它使用JSON而不是旧的GData协议提供联系人和个人资料信息。
而不是代码示例,这里是Java quickstart& documentaion
build.gradle
的依赖关系应为:
dependencies {
implementation 'com.google.api-client:google-api-client:1.23.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
implementation 'com.google.apis:google-api-services-people:v1-rev187-1.23.0'
}
顺便说一句,这不是CountryZipCode
- 而是国家(条目)代码。