Android手機衛士之擷取連絡人資訊顯示與回顯_Android

來源:互聯網
上載者:User

前面的文章已經實現相關的布局,本文接著進行相關的功能實現

讀取系統連絡人
當點擊“選擇連絡人”按鈕後,彈出連絡人清單,讀取系統連絡人分如下幾個步驟:

系統連絡人提供了一個內容提供者,通過內容解析器,匹配Url地址

1.內容解析器

2.Url地址,查看系統連絡人資料庫,內容提供者源碼

先看api文檔的資訊清單檔,後看java類(連絡人資料庫有多張表)

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

3.系統連絡人資料庫中核心表的表結構

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,將特殊字元過濾(中劃線轉換成Null 字元串)      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);      }    });  }

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.