Android 手機衛士--獲取聯繫人信息並顯示與回顯

前面的文章已經實現相關的佈局,本文接着進行相關的功能實現html

本文地址:http://www.cnblogs.com/wuyudong/p/5951794.html,轉載請註明出處。java

讀取系統聯繫人

當點擊「選擇聯繫人」按鈕後,彈出聯繫人列表,讀取系統聯繫人分以下幾個步驟:android

系統聯繫人提供了一個內容提供者,經過內容解析器,匹配Url地址數據庫

1,內容解析器api

2,Url地址,查看系統聯繫人數據庫,內容提供者源碼ide

先看api文檔的清單文件,後看java類(聯繫人數據庫有多張表)佈局

contents://com.android.contacts/表名this

3,系統聯繫人數據庫中核心表的表結構spa

raw_contacts 聯繫人表: contact_id 聯繫人惟一性id值

data 用戶信息表:raw_contact_id做爲外鍵,和raw_contacts中contact_id作關聯查詢線程

獲取data1字段,包含了電話號碼以及聯繫人名稱

mimetype_id字段,包含了當前行data1對應的數據類型

mimetypes 類型表: 獲取data表中mimetype_id和mimetypes中_id作關聯查詢,獲取指向的信息類型
電話號碼:vnd.android.cursor.item/phone_v2
用戶名稱:vnd.android.cursor.item/name

4,表的訪問方式

content://com.android.contacts/raw_contacts
content://com.android.contacts/data

下面用代碼實現

    private ListView lv_contact;
    private List<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
    private MyAdapter mAdapter;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //8,填充數據適配器
            mAdapter = new MyAdapter();
            lv_contact.setAdapter(mAdapter);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_list);
        initUI();
        initData();
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return contactList.size();
        }

        @Override
        public HashMap<String, String> getItem(int i) {
            return contactList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = View.inflate(getApplicationContext(), R.layout.listview_contact_item, null);
            TextView tv_name = (TextView)v.findViewById(R.id.tv_name);
            TextView tv_phone = (TextView)v.findViewById(R.id.tv_phone);
            tv_name.setText(getItem(i).get("name"));
            tv_phone.setText(getItem(i).get("phone"));
            return v;
        }
    }

    /**
     *  獲取聯繫人數據的方法
     */
    private void initData() {
        //由於讀取系統聯繫人,多是一個耗時操做,放置到子線程中處理
        new Thread(){
            public void run(){
                //1,獲取內容解析器對象
                ContentResolver contentResolver = getContentResolver();
                //2,作查詢系統聯繫人數據庫表過程(讀取聯繫人權限)
                Cursor cursor = contentResolver.query(
                        Uri.parse("content://com.android.contacts/raw_contacts"),
                        new String[]{"contact_id"},
                        null, null, null);
                contactList.clear();
                //3,循環遊標,直到沒有數據爲止
                while (cursor.moveToNext()){
                    String id = cursor.getString(0);
                    //4,根據用戶惟一性id值,查詢data表和mimetype表生成的視圖,獲取data以及mimetype字段
                    Cursor indexCursor = contentResolver.query(
                            Uri.parse("content://com.android.contacts/data"),
                            new String[]{"data1","mimetype"},
                            "raw_contact_id = ?", new String[]{id}, null);
                    //5,循環獲取每個聯繫人的電話號碼以及姓名,數據類型
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    while (indexCursor.moveToNext()){
                        String data = indexCursor.getString(0);
                        String type = indexCursor.getString(1);

                        //6,區分類型去給hashMap填充數據
                        if(type.equals("vnd.android.cursor.item/phone_v2")) {
                            //數據非空判斷
                            if(!TextUtils.isEmpty(data)) {
                                hashMap.put("phone", data);
                            }
                        }else if(type.equals("vnd.android.cursor.item/name")) {
                            if(!TextUtils.isEmpty(data)) {
                                hashMap.put("name", data);
                            }
                        }
                    }
                    indexCursor.close();
                    contactList.add(hashMap);

                }
                cursor.close();
                //7,消息機制,發送一個空的消息,告知主線程能夠去使用子線程已經填充好的數據集合
                mHandler.sendEmptyMessage(0);
            }

        }.start();
    }

實現的效果以下:

聯繫人信息回顯

接下來實現點擊聯繫人條目,實現回顯,例如雙擊第一個條目,號碼自動添加

代碼以下:

    private void initUI() {
        lv_contact = (ListView) findViewById(R.id.lv_contact);
        lv_contact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //1,獲取點中條目的索引指向集合中的對象
                if(mAdapter != null) {
                    HashMap<String, String> hashMap = mAdapter.getItem(i);
                    //2,獲取當前條目指向集合對應的電話號碼
                    String phone =  hashMap.get("phone");
                    //3,此電話號碼須要給第三個導航界面使用

                    //4,在結束此界面回到前一個導航界面的時候,須要將數據返回過去
                    Intent intent = new Intent();
                    intent.putExtra("phone", phone);
                    setResult(0, intent);
                    finish();

                }
            }
        });
    }

接着onActivityResult中添加下面的代碼

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data != null) {
            //1,返回到當前界面的時候,接受結果的方法
            String phone = data.getStringExtra("phone");
            //2,將特殊字符過濾(中劃線轉換成空字符串)
            phone = phone.replace("-", "").replace(" ", "").trim();
            et_phone_number.setText(phone);

            //3,存儲聯繫人至sp中
 SpUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE, phone);         }
        super.onActivityResult(requestCode, resultCode, data);
    }

當填寫號碼後,進入下一頁,再次返回,發現號碼不見了,因而使用sp存儲並從中讀取

    private void initUI() {
        //顯示電話號碼的輸入框
        et_phone_number = (EditText)findViewById(R.id.et_phone_number);
        //獲取聯繫人電話號碼回顯過程
        String contact_phone = SpUtil.getString(this, ConstantValue.CONTACT_PHONE, ""); et_phone_number.setText(contact_phone);         bt_select_number = (Button) findViewById(R.id.bt_select_number);
       //點擊選擇聯繫人的對話框
        bt_select_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), ContactListActivity.class);
                startActivityForResult(intent, 0);
            }
        });
    }
相關文章
相關標籤/搜索