Android联系人Content Provider返回同一联系人的多个副本,联系人中没有重复的联系人

时间:2014-06-28 11:53:39

标签: android contacts

public static String getContactName(Context context, String id, String address){

        if(id==null){
            if(address==null){
                return null;
            }else{
                return PhoneNumberUtils.formatNumber(address);
            }
        }

        Cursor cursor = context.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI, 
                null, 
                null, 
                null, 
                null);

        if(cursor!=null){
            try{
                if(cursor.moveToFirst()){
                    do{
                        for(int i=0;i<cursor.getColumnCount();++i){
                            String columnName = cursor.getColumnName(i);
                            String columnText = cursor.getString(i);
                            Log.d(columnName, columnText==null ? "null":columnText);
                        }
                        Log.d("endline","***************************");
                    }while(cursor.moveToNext());


                }else{
                    Log.d("cursor", "not greater than 0");
                }
            }finally{
                cursor.close();
            }
        }

        if(address != null){
            return PhoneNumberUtils.formatNumber(address);
        }

        return "just testing shit";
    }

所以我的问题是,当我退回所有联系人时,我会得到每个联系人的多个副本,我不知道为什么,或者该怎么做。我在网上查看了我的联系人:https://www.google.com/contacts/#contacts

除了删除手机中的所有联系人并重新加入谷歌联系人以确保没有重复。我无法理解发生了什么,虽然我可能只是在某种程度上是愚蠢的。

以下是输出的内容(截断为几个联系人)

第一个:

sort_key: James LaChance
photo_uri: null
status_label: null
status_ts: null
status_res_package: null
name_verified: 0
display_name: James LaChance
last_time_used: null
mimetype: vnd.android.cursor.item/photo
phonebook_label_alt: L
data6: null
version: 3
photo_id: null
data3: null
custom_ringtone: null
times_contacted: 0
account_type_and_data_set: com.google
dirty: 0
data7: null
data15: null
raw_contact_is_user_profile: 0
data_set: null
phonebook_label: J
data10: null
res_package: null
account_type: com.google
data11: null
display_name_alt: LaChance, James
lookup: 3448i1629e6890832873d
phonetic_name: null
last_time_contacted: 0
contact_last_updated_timestamp: 1403954657613
data13: null
in_visible_group: 1
chat_capability: null
data9: null
data_sync1: https://www.google.com/m8/feeds/photos/media/jmsmaupin1%40gmail.com/1629e6890832873d  
sort_key_alt: LaChance, James
contact_presence: null
data_version: 0
phonetic_name_style: 0
name_raw_contact_id: 1
raw_contact_id: 1
send_to_voicemail: 0
data4: null
data12: null
contact_status: null
contact_status_label: null
pinned: 2147483647
status_icon: null
status: null
data1: null
phonebook_bucket: 10
data_sync2: null
contact_status_res_package: null
in_default_directory: 1
_id: 1
is_super_primary: 0
data5: null
contact_id: 8
data8: null
is_primary: 0
data_sync4: 0
has_phone_number: 1
display_name_source: 40
photo_file_id: null
data_sync3: null
data14: null
contact_status_ts: null
phonebook_bucket_alt: 12
mode: null
data2: null
group_sourceid: null
starred: 0
photo_thumb_uri: null
times_used: null
contact_status_icon: null
contact_chat_capability: null
sourceid: 1629e6890832873d

第二个:

sort_key: James LaChance
photo_uri: null
status_label: null
status_ts: null
status_res_package: null
name_verified: 0
display_name: James LaChance
last_time_used: null
mimetype: vnd.android.cursor.item/name
phonebook_label_alt: L
data6: null
version: 3
photo_id: null
data3: LaChance
custom_ringtone: null
times_contacted: 0
account_type_and_data_set: com.google
dirty: 0
data7: null
data15: null
raw_contact_is_user_profile: 0
data_set: null
phonebook_label: J
data10: 1
res_package: null
account_type: com.google
data11: 0
display_name_alt: LaChance, James
lookup: 3448i1629e6890832873d
phonetic_name: null
last_time_contacted: 0
contact_last_updated_timestamp: 1403954657613
data13: null
in_visible_group: 1
chat_capability: null
data9: null
data_sync1: null
sort_key_alt: LaChance, James
contact_presence: null
data_version: 0
phonetic_name_style: 0
name_raw_contact_id: 1
raw_contact_id: 1
send_to_voicemail: 0
data4: null
data12: null
contact_status: null
contact_status_label: null
pinned: 2147483647
status_icon: null
status: null
data1: James LaChance
phonebook_bucket: 10
data_sync2: null
contact_status_res_package: null
in_default_directory: 1
_id: 2
is_super_primary: 0
data5: null
contact_id: 8
data8: null
is_primary: 0
data_sync4: 10
has_phone_number: 1
display_name_source: 40
photo_file_id: null
data_sync3: null
data14: null
contact_status_ts: null
phonebook_bucket_alt: 12
mode: null
data2: James
group_sourceid: null
starred: 0
photo_thumb_uri: null
times_used: null
contact_status_icon: null
contact_chat_capability: null
sourceid: 1629e6890832873d
我是个白痴。在我提出这个问题后,我几乎立即找到了问题的答案。

请参阅:https://stackoverflow.com/a/3591323/2408279

基本上,每种联系类型都有一个参考I.E.电子邮件,电话等(如果我没记错的话)

1 个答案:

答案 0 :(得分:0)

您应该查询public class DownloadTask { private static final String TAG = "Download Task"; private Context context; private Button buttonText; private String downloadUrl = "", downloadFileName = ""; public DownloadTask(Context context, String downloadUrl) { this.context = context; this.buttonText = buttonText; this.downloadUrl = downloadUrl; downloadFileName = downloadUrl.replace("", "");//Create file name by picking download file name from URL Log.e(TAG, downloadFileName); //Start Downloading Task new DownloadingTask().execute(); } private class DownloadingTask extends AsyncTask<Void, Void, Void> { File apkStorage = null; File outputFile = null; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(Void result) { try { if (outputFile != null) { buttonText.setEnabled(true); Log.e("57","download"+"completed"); } else { new Handler().postDelayed(new Runnable() { @Override public void run() { Log.e("62","download again"); } }, 3000); Log.e(TAG, "Download Failed"); } } catch (Exception e) { e.printStackTrace(); Log.e("72,","download failed"); //Change button text if exception occurs new Handler().postDelayed(new Runnable() { @Override public void run() { Log.e("80","download again"); } }, 3000); Log.e(TAG, "Download Failed with Exception - " + e.getLocalizedMessage()); } super.onPostExecute(result); } @Override protected Void doInBackground(Void... arg0) { try { URL url = new URL(downloadUrl);//Create Download URl HttpURLConnection c = (HttpURLConnection) url.openConnection();//Open Url Connection c.setRequestMethod("GET");//Set Request Method to "GET" since we are grtting data c.connect();//connect the URL Connection //If Connection response is not OK then show Logs if (c.getResponseCode() != HttpURLConnection.HTTP_OK) { Log.e(TAG, "Server returned HTTP " + c.getResponseCode() + " " + c.getResponseMessage()); } //Get File if SD card is present if (new CheckForSDCard().isSDCardPresent()) { apkStorage = new File(Environment.getExternalStorageDirectory() + "/" + "downloadDirectory"); } else Toast.makeText(context, "Oops!! There is no SD Card.", Toast.LENGTH_SHORT).show(); //If File is not present create directory if (!apkStorage.exists()) { apkStorage.mkdir(); Log.e(TAG, "Directory Created."); } outputFile = new File(apkStorage, downloadFileName);//Create Output file in Main File //Create New File if not present if (!outputFile.exists()) { outputFile.createNewFile(); Log.e(TAG, "File Created"); } FileOutputStream fos = new FileOutputStream(outputFile);//Get OutputStream for NewFile Location InputStream is = c.getInputStream();//Get InputStream for connection byte[] buffer = new byte[1024];//Set buffer type int len1 = 0;//init length while ((len1 = is.read(buffer)) != -1) { fos.write(buffer, 0, len1);//Write new file } //Close all connection after doing task fos.close(); is.close(); } catch (Exception e) { //Read exception if something went wrong e.printStackTrace(); outputFile = null; Log.e(TAG, "Download Error Exception " + e.getMessage()); } return null; } } } ContactsContract.CommonDataKinds.Phone表格,让我知道它是否有帮助。

正如pskink所说,使用CONTENT_URI来记录光标..更简单