欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android手機(jī)衛(wèi)士之獲取聯(lián)系人信息顯示與回顯

 更新時(shí)間:2016年10月12日 16:02:07   作者:wuyudong  
這篇文章主要介紹了Android手機(jī)衛(wèi)士之獲取聯(lián)系人信息顯示與回顯的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前面的文章已經(jīng)實(shí)現(xiàn)相關(guān)的布局,本文接著進(jìn)行相關(guān)的功能實(shí)現(xiàn)

讀取系統(tǒng)聯(lián)系人
當(dāng)點(diǎn)擊“選擇聯(lián)系人”按鈕后,彈出聯(lián)系人列表,讀取系統(tǒng)聯(lián)系人分如下幾個(gè)步驟:

系統(tǒng)聯(lián)系人提供了一個(gè)內(nèi)容提供者,通過內(nèi)容解析器,匹配Url地址

1.內(nèi)容解析器

2.Url地址,查看系統(tǒng)聯(lián)系人數(shù)據(jù)庫,內(nèi)容提供者源碼

先看api文檔的清單文件,后看java類(聯(lián)系人數(shù)據(jù)庫有多張表)

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

3.系統(tǒng)聯(lián)系人數(shù)據(jù)庫中核心表的表結(jié)構(gòu)

raw_contacts 聯(lián)系人表: contact_id 聯(lián)系人唯一性id值

data 用戶信息表:raw_contact_id作為外鍵,和raw_contacts中contact_id做關(guān)聯(lián)查詢

獲取data1字段,包含了電話號(hào)碼以及聯(lián)系人名稱

mimetype_id字段,包含了當(dāng)前行data1對(duì)應(yīng)的數(shù)據(jù)類型

mimetypes 類型表: 獲取data表中mimetype_id和mimetypes中_id做關(guān)聯(lián)查詢,獲取指向的信息類型
電話號(hào)碼:vnd.android.cursor.item/phone_v2
用戶名稱:vnd.android.cursor.item/name

4.表的訪問方式

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

下面用代碼實(shí)現(xiàn)

  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,填充數(shù)據(jù)適配器
      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;
    }
  }

  /**
   * 獲取聯(lián)系人數(shù)據(jù)的方法
   */
  private void initData() {
    //因?yàn)樽x取系統(tǒng)聯(lián)系人,可能是一個(gè)耗時(shí)操作,放置到子線程中處理
    new Thread(){
      public void run(){
        //1,獲取內(nèi)容解析器對(duì)象
        ContentResolver contentResolver = getContentResolver();
        //2,做查詢系統(tǒng)聯(lián)系人數(shù)據(jù)庫表過程(讀取聯(lián)系人權(quán)限)
        Cursor cursor = contentResolver.query(
            Uri.parse("content://com.android.contacts/raw_contacts"),
            new String[]{"contact_id"},
            null, null, null);
        contactList.clear();
        //3,循環(huán)游標(biāo),直到?jīng)]有數(shù)據(jù)為止
        while (cursor.moveToNext()){
          String id = cursor.getString(0);
          //4,根據(jù)用戶唯一性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,循環(huán)獲取每一個(gè)聯(lián)系人的電話號(hào)碼以及姓名,數(shù)據(jù)類型
          HashMap<String, String> hashMap = new HashMap<String, String>();
          while (indexCursor.moveToNext()){
            String data = indexCursor.getString(0);
            String type = indexCursor.getString(1);

            //6,區(qū)分類型去給hashMap填充數(shù)據(jù)
            if(type.equals("vnd.android.cursor.item/phone_v2")) {
              //數(shù)據(jù)非空判斷
              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,消息機(jī)制,發(fā)送一個(gè)空的消息,告知主線程可以去使用子線程已經(jīng)填充好的數(shù)據(jù)集合
        mHandler.sendEmptyMessage(0);
      }

    }.start();
  }

實(shí)現(xiàn)的效果如下:

聯(lián)系人信息回顯

接下來實(shí)現(xiàn)點(diǎn)擊聯(lián)系人條目,實(shí)現(xiàn)回顯,例如雙擊第一個(gè)條目,號(hào)碼自動(dòng)添加

代碼如下:

  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,獲取點(diǎn)中條目的索引指向集合中的對(duì)象
        if(mAdapter != null) {
          HashMap<String, String> hashMap = mAdapter.getItem(i);
          //2,獲取當(dāng)前條目指向集合對(duì)應(yīng)的電話號(hào)碼
          String phone = hashMap.get("phone");
          //3,此電話號(hào)碼需要給第三個(gè)導(dǎo)航界面使用

          //4,在結(jié)束此界面回到前一個(gè)導(dǎo)航界面的時(shí)候,需要將數(shù)據(jù)返回過去
          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,返回到當(dāng)前界面的時(shí)候,接受結(jié)果的方法
      String phone = data.getStringExtra("phone");
      //2,將特殊字符過濾(中劃線轉(zhuǎn)換成空字符串)
      phone = phone.replace("-", "").replace(" ", "").trim();
      et_phone_number.setText(phone);

      //3,存儲(chǔ)聯(lián)系人至sp中
      SpUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE, phone);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

當(dāng)填寫號(hào)碼后,進(jìn)入下一頁,再次返回,發(fā)現(xiàn)號(hào)碼不見了,于是使用sp存儲(chǔ)并從中讀取

  private void initUI() {
    //顯示電話號(hào)碼的輸入框
    et_phone_number = (EditText)findViewById(R.id.et_phone_number);
    //獲取聯(lián)系人電話號(hào)碼回顯過程
    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);
    //點(diǎn)擊選擇聯(lián)系人的對(duì)話框
    bt_select_number.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent intent = new Intent(getApplicationContext(), ContactListActivity.class);
        startActivityForResult(intent, 0);
      }
    });
  }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論