0

我正在制作一个应用程序,我希望将所有记录从我的 android mobile 4.0 保存到我的 android 应用程序。我也这样做了。但问题是我的电话簿中有近 200 个联系人,但我的应用程序中随机只有 90 条记录。我已经尝试了很多。但我没有找到任何解决方案。任何人都有解决方案吗?下面是我的代码:

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 pnumber 
        =pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    contactids.add(id);
    names.add(name.trim().toLowerCase());
    PhoneNumbers.add(pnumber);
    } 
       pCur.close();
}
else
{
    String pnumber="";
    contactids.add(id);
    names.add(name);
    PhoneNumbers.add(pnumber);
}
}

最后我完成了以下代码。但是这样做的问题是如果联系人超过 1500 则它无法获取记录。并且直到 1500 条记录的过程非常缓慢。

public class MyActivity extends Activity {
public Cursor cur;
public int j=0;
@Override
public void onCreate(Bundle savedInstanceState) 
{
     cur=getContacts();
    startManagingCursor(cur);
    cur.moveToFirst(); 
    Button btn=(Button)findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() 
    {   
        public void onClick(View v) 
        {               
            contactids=new ArrayList<String>();
            names=new ArrayList<String>();
            PhoneNumbers=new ArrayList<String>();
            try
            {
                new showdialog1(MyActivity.this).execute();
            }
            catch (Exception e) 
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        }
    });
}
class showdialog1 extends AsyncTask<Void, Void, Void>
{
    public showdialog1(Activity act) {
         super.onPreExecute();
    }   
    @Override
    protected void onPreExecute() {

        dialog = ProgressDialog.show(MyActivity.this,"title", "message");
    }

    @Override
    protected Void doInBackground(Void... params) {
        if (cur.getCount() > 0) 
        {
            do
            {
                String id = cur.getString(
                cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(
                                cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"=" +id, null, null);
                if(pCur.getCount()>0)
                {
                    while (pCur.moveToNext()) 
                        {
                        // Do something with phones
                            String pnumber=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            contactids.add(id);
                            names.add(name.trim().toLowerCase());
                            PhoneNumbers.add(pnumber);
                        } 
                        pCur.close();
            }
            else
            {
                String pnumber="";
                    contactids.add(id);
                    names.add(name.trim().toLowerCase());
                    PhoneNumbers.add(pnumber);
            }

           }while (cur.moveToNext()); 
            cur.close();
        }               
        return null;        
    }
    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        int contactidsize=contactids.size();
        int namesize=contactids.size();
        int numbersize=contactids.size();
        saverecords();
        return;
    }       

}
}
4

3 回答 3

2

你得到你手机的联系人列表..试试这个代码,然后如果你想使用列表中的联系人,你选择那个联系人并使用该联系人的详细信息(例如:号码,姓名,email.id等)请转到下面的链接

/***  USE this CODE   ******/
int PICK_CONTACT=1;

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);`
于 2012-10-12T11:38:30.033 回答
1

您正在使用以下条件:

Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER ))) > 0)

这不考虑没有电话号码的联系人。

希望这有效

于 2012-08-14T13:47:17.103 回答
-1

请参阅 ContactManager http://developer.android.com/resources/samples/ContactManager/index.html

创建并初始化布尔变量mShowInvisible- 如果设置为true,它将列出所有联系人,而不管用户偏好如何

/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts(){
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
    ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
于 2012-08-07T06:23:05.573 回答