在之前項目中有用到關(guān)于獲取手機聯(lián)系人的部分,閑置就想和大家分享一下,話不多說,上代碼:
成都創(chuàng)新互聯(lián)公司主營北戴河網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),北戴河h5成都微信小程序搭建,北戴河網(wǎng)站營銷推廣歡迎北戴河等地區(qū)企業(yè)咨詢java部分:
package com.example.content; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { private ContentResolver cr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取內(nèi)容訪問者 cr = getContentResolver(); } public void getContacts(View view){ Uri uri=Uri.parse("content://com.android.contacts/raw_contacts"); Cursor cursor=cr.query(uri,null,null,null,null); while(cursor.moveToNext()){ int _id=cursor.getInt(cursor.getColumnIndex("_id")); String display_name=cursor.getString(cursor.getColumnIndex("display_name")); Log.i("test",_id+" "+display_name); Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+_id+"/data"); Cursor cursorData=cr.query(uriData,null,null,null,null); while(cursorData.moveToNext()){ String mimetype=cursorData.getString(cursorData.getColumnIndex("mimetype")); String data1=cursorData.getString(cursorData.getColumnIndex("data1")); if("vnd.android.cursor.item/phone_v2".equals(mimetype)){ Log.i("test"," "+mimetype+" "+data1); } } } } }