如何查看给定的电话号码是否保存在我的联系人中,消耗时间短?

时间:2014-05-30 18:27:58

标签: java android for-loop contactscontract

我在远程服务器中保存了一些联系号码,我的Android手机上保存了100多个号码。

我想要做的是比较远程服务器上的号码和Android手机中的电话号码,并在Listview中仅显示匹配的号码,即1234是否保存在我的远程服务器上,1234,5678,1000,等等保存在我的Android手机上,然后Listview上只显示数字1234。

我做了以下编码,它工作正常并给我我想要的结果,但问题是时间消耗非常高。

有人可以建议我是否可以在不使用循环的情况下获得匹配的联系人,或者有没有办法减少时间消耗? 我的代码如下,请一步一步指导我。

AsyncTask<Void, Void,Void> t = new AsyncTask<Void, Void,Void>()
{

    @Override
    protected Void doInBackground(Void... arg0) {
        String phoneNumber = null;
        //String email = null;
        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

        StringBuffer output = new StringBuffer();
        if(getActivity()!=null)
        {
            ContentResolver contentResolver =getActivity().getApplicationContext().getContentResolver();
            //contentResolver=getActivity().getContentResolver().getContentResolver();
            Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
                    String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
                    int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
                    output.append("\n First Name:" + name);
                    Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
                    while (phoneCursor.moveToNext()) {
                        phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                        String p=phoneNumber;
                        output.append("\n Phone number:" + phoneNumber);
                        String result = null;
                        InputStream is = null;
                        try{
                            HttpClient httpclient = new DefaultHttpClient();
                            HttpPost httppost = new HttpPost("http://10.0.2.2:80/contactcheck.php");

                            HttpResponse response1 = httpclient.execute(httppost);
                            HttpEntity entity = response1.getEntity();
                            is = entity.getContent();
                            Log.e("log_tag", "connection success ");
                        }
                        catch(Exception e)
                        {
                            Log.e("log_tag", "Error in http connection "+e.toString());
                        }
                        try
                        {
                            BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8);
                            StringBuilder sb = new StringBuilder();
                            String line = null;
                            while ((line = reader.readLine()) != null)
                            {
                                sb.append(line + "\n");
                            }
                            is.close();
                            result=sb.toString();
                        }
                        catch(Exception e)
                        {
                            Log.e("log_tag", "Error converting result "+e.toString());
                        }
                        try
                        {
                            JSONArray jArray = new JSONArray(result);
                            String s11,s12,s13;
                            Log.w("Lengh",""+jArray.length());
                            for(int i=0;i<jArray.length();i++)
                            {
                                JSONObject json_data = jArray.getJSONObject(i);
                                s11=json_data.getString("PhoneId");
                                s12=json_data.getString("path");
                                s13=json_data.getString("UserId");
                                Log.w("matched",""+s11+p);
                                Log.i("path from json",""+s12);

                                if(p.compareTo(s11)==0){
                                    int count=0;
                                    count=db.getcountoffriends(s11);
                                    if(count==0)
                                    {
                                        queryValues.put("c",s11);
                                        queryValues.put("name",name);
                                        queryValues.put("path", s12);
                                        queryValues.put("fbid",s13);

                                        db.insertcontact(queryValues);
                                        imageload(s12,s13,s11);

                                    }




                                    Log.i("imageupload strings",""+s12+""+s13+""+s11);
                                }
                            }
                        }
                        catch(JSONException e)
                        {
                            Log.e("log_tag", "Error parsing data "+e.toString());
                        }
                    }
                }
            }
        }
        return null;
    }
};
t.execute();

1 个答案:

答案 0 :(得分:1)

从远程服务器获取每个电话号码并传递给以下方法:

public boolean contactExists(Context context, String number) {
    /// number is the phone number
    Uri lookupUri = Uri.withAppendedPath(
    PhoneLookup.CONTENT_FILTER_URI, 
    Uri.encode(number));
    String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
    Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
    try {
        if (cur.moveToFirst()) {
            return true;
        }
    } finally {
    if (cur != null)
        cur.close();
    } 
    return false;
 }

如果方法返回true,则存储相关数据以显示在ListView

原始答案Android : Check phone number present in Contact List ? (Phone number retrieve from phone call)