我想要获取手机中保存的手机联系人、SIM联系人和Google联系人,但我只能获取手机和SIM联系人。如果我可以离线访问谷歌联系人,它必须存储在本地数据库的某个地方。
我怎样才能把它们全部导入?我找遍了所有的地方,但没有找到一个解决方案。
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);
}
}
}
发布于 2017-12-11 10:27:47
同时,与之前的Contacts API相比,People API是推荐的方法:
要对用户的联系人进行读写访问,请使用People
,它使用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
--而是一个国家(入口)代码。
https://stackoverflow.com/questions/47750440
复制