获取手机联系人(Contentprovider)

1.配置系统权限





2.主函数

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private ArrayList> arr;
    private SimpleAdapter adap;
    private Uri uri1= ContactsContract.RawContacts.CONTENT_URI;
    private Uri uri2=ContactsContract.Data.CONTENT_URI;
    private Uri phone=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    private Uri email=ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    private ContentResolver resolver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        resolver=getContentResolver();
        arr = new ArrayList<>();
        arr.addAll(aa());
        adap = new SimpleAdapter(this,arr, R.layout.buju,new String[] {"display_name", "phone", "email" },new int[] { R.id.tv1, R.id.tv2, R.id.tv3});
        lv.setAdapter(adap);
    }

    public void cc(View view) {
        adap.notifyDataSetChanged();
    }
    public ArrayList> aa(){
        ArrayList> arr = new ArrayList<>();
        Cursor query = resolver.query(uri1, new String[]{"_id", "display_name"}, null, null, null);
        if(query!=null){
            while (query.moveToNext()){
                HashMap map = new HashMap<>();
                String str0 = query.getString(0);
                String str1 = query.getString(1);
                map.put("rawld",str0);
                map.put("display_name",str1);

                Cursor query1 = resolver.query(phone, new String[]{"data1"}, "raw_contact_id = ?", new String[]{str0}, null);
                if(query1!=null){
                    while (query1.moveToNext()){
                        String phone = query1.getString(0);
                        map.put("phone",phone);
                    }
                }
                Cursor query2 = resolver.query(email, new String[]{"data1"}, "raw_contact_id = ?", new String[]{str0}, null);
                if (query2 != null) {
                    while (query2.moveToNext()) {
                        String email = query2.getString(0);
                        map.put("email", email);
                    }
                }
                query2.close();
                arr.add(map);
            }
            query.close();
        }
        return arr;
    }



}





3.主布局



    





4.item布局



    


你可能感兴趣的:(获取手机联系人(Contentprovider))