如何将我的Android手机联系人的联系人同步到我的Android应用程序,如Whatsapp和Skype

时间:2015-02-18 06:03:27

标签: android ios

我正在制作一个类似于whatsapp的Android应用程序我想知道的是如何将android联系人与我的应用程序同步,以便在我的应用程序中注册的数字也在我的android联系人中得到了一个符号。例如,在您的联系人列表中,您会看到skype图标或whatsapp图标,其中包含使用skype或whatsapp注册的联系人。 还想在我的应用程序的联系人中显示该联系人。 任何人都可以指导我做什么。 谢谢!提前......

2 个答案:

答案 0 :(得分:0)

试试这个

public class ContactActivity extends Activity {

Cursor cursor;
ArrayList<String> NameList=new ArrayList<String>();
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView=(ListView)findViewById(R.id.listveiew_lv);

//  NameList=new ArrayList<String>();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,NameList);
    cursor = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);

    while (cursor.moveToNext()) {
        int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);   
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);

        String name = cursor.getString(nameIdx);
        String phone = cursor.getString(phoneNumberIdx);    
        NameList.add(phone);

        System.out.println("Name is :"+name +" number is : "+phone);
        System.out.println("Name is :"+name +" number is : "+phone);
        System.out.println("Name is :"+name +" number is : "+phone);



    }

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), NameList.get(arg2), 1).show();
        }
    });
}

public void onClick(View arg0) {
    // TODO Auto-generated method stub

    Intent i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    super.startActivityForResult(i, 1001);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case 1001:

        if (resultCode == Activity.RESULT_OK) {

            Cursor s = getContentResolver().query(Phone.CONTENT_URI, null,
                    null, null, null);

            if (s.moveToFirst()) {
                String phoneNum = s.getString(s.getColumnIndex(Phone.NUMBER));
                Toast.makeText(getBaseContext(), phoneNum, Toast.LENGTH_LONG).show();
            }

        }

        break;

    }

}

}

答案 1 :(得分:0)

Cursor c = getContentResolver().query(
    RawContacts.CONTENT_URI,
    new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
    RawContacts.ACCOUNT_TYPE + "= ?",
    new String[] { "com.whatsapp" },
    null);

ArrayList<String> myWhatsappContacts = new ArrayList<String>();
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
while (c.moveToNext())
{
 // You can also read RawContacts.CONTACT_ID to read the
 // ContactsContract.Contacts table or any of the other related ones.
 myWhatsappContacts.add(c.getString(contactNameColumn));
}

使用您的应用程序名称更改“com.whatsapp”,但您首先需要注册 为此,请参阅http://developer.android.com/guide/topics/providers/contacts-provider.html