Android读取选择联系人的模版代码实现

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1、创建SelectContactActivity类,在AndroidManifest.xml添加类

    import java.util.ArrayList;  
    import java.util.HashMap;  
    import java.util.List;  
    import java.util.Map;  
      
    import android.app.Activity;  
    import android.content.ContentResolver;  
    import android.content.Intent;  
    import android.database.Cursor;  
    import android.net.Uri;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.widget.AdapterView;  
    import android.widget.AdapterView.OnItemClickListener;  
    import android.widget.ListView;  
    import android.widget.SimpleAdapter;  
      
    public class SelectContactActivity extends Activity {  
      
        private ListView lv_select_contact;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            // TODO Auto-generated method stub  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_select_contact);  
            lv_select_contact = (ListView) findViewById(R.id.lv_select_contact);  
            final List<Map<String,String>> data = getContactInfo();  
            lv_select_contact.setAdapter(new SimpleAdapter(this, data, R.layout.contact_item_view,  
                    new String[]{"name","phone"}, new int[]{R.id.tv_name,R.id.tv_phone}));  
            lv_select_contact.setOnItemClickListener(new OnItemClickListener() {  
      
                @Override  
                public void onItemClick(AdapterView<?> parent, View view, int position,  
                        long id) {  
                    String phone = data.get(position).get("phone");  
                    Intent data = new Intent();  
                    data.putExtra("phone", phone);  
                    setResult(0, data);  
                    //关闭掉当前的页面  
                    finish();  
                }  
                  
            });  
        }  
          
        /** 
         * 读取手机里面的联系人 
         * @return 
         */  
        private List<Map<String, String>> getContactInfo() {  
            List<Map<String, String>> list = new ArrayList<Map<String,String>>();  
              
            //得到一个内容解析器  
            ContentResolver resolver = getContentResolver();  
            //raw_contact uri  
            Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");  
            Uri uriData = Uri.parse("content://com.android.contacts/data");  
              
            Cursor cursor = resolver.query(uri, new String[]{"contact_id"}, null, null, null);  
            while(cursor.moveToNext()){  
                  
                String contact_id = cursor.getString(0);  
                  
                if(contact_id != null){  
                    //具体的某一个联系人  
                    Map<String, String> map =new HashMap<String, String>();  
                    Cursor dataCursor = resolver.query(uriData, new String[]{"data1","mimetype"}, "contact_id=?", new String[]{contact_id}, null);  
                    while(dataCursor.moveToNext()){  
                        String data1 = dataCursor.getString(0);  
                        String mimetype = dataCursor.getString(1);  
                        System.out.println("data1=="+data1+"mimetype=="+mimetype);  
                          
                        if("vnd.android.cursor.item/name".equals(mimetype)){  
                            //联系人的姓名  
                            map.put("name", data1);  
                        }else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){  
                            //联系人的电话号码  
                            map.put("phone", data1);  
                        }  
                    }  
                    list.add(map);  
                    dataCursor.close();  
                }  
            }  
            cursor.close();  
            return list;  
        }  
    }  

2、读取联系人需要获得权限

添加权限:<uses-permission android:name="android.permission.READ_CONTACTS"/>

3、前面java需要引入相关layout,模版如下注意姓名号码id:

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical" >  
      
        <TextView  
            android:background="#8866ff00"  
            android:gravity="center"  
            android:textColor="#000000"  
            android:textSize="20sp"  
            android:id="@+id/textView1"  
            android:layout_width="fill_parent"  
            android:layout_height="55dip"  
            android:text="选择联系人"/>  
        
        <ListView   
            android:verticalSpacing="10dip"  
            android:layout_marginTop="15dip"  
            android:numColumns="3"  
            android:id="@+id/lv_select_contact"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            />  
      
    </LinearLayout>  

4、实现选择联系人的点击事件

xml的Button中 android:onClick="selectContact" ,注意返回startActivityForresult()方法

     * 选择联系人的点击事件  
     */  
    public void selectContact(View view){  
        Intent intent = new Intent(this,SelectContactActivity.class);  
        startActivityForResult(intent, 0);  
    }  

5、然后将点击的联系人投射到号码输入框

    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        // TODO Auto-generated method stub  
        super.onActivityResult(requestCode, resultCode, data);  
          
        if(data == null){  
            return ;  
        }  
        //et_setup3_phone是输入框的id  
        String phone = data.getStringExtra("phone").replace("-", "");  
        et_setup3_phone.setText(phone);  
    }  

6、最后说下注意点

在获取完号码后,整个流程就结束了,在再次引用号码时最好toString().trim()操作,然后如果有必要可以判空和保存,保存要注意commit()。