国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android利用ContentResolver訪問者獲取手機聯系人信息

2019-12-12 03:40:03
字體:
來源:轉載
供稿:網友

利用ContentResolver內容訪問者,獲取手機聯系人信息我做了兩種不同的做法。第一種,直接獲取所有手機聯系人信息,展示在ListView中。第二種,通過Butten按鈕跳轉到系統的手機聯系人界面,單個獲取手機聯系人信息,展示在ListView中,結果如下:

第一種:

這里寫圖片描述

第二種:

這里寫圖片描述

第一種:直接獲取所有手機聯系人信息

首先需要在AndroidManifest.xml文件中添加權限:

<uses-permission android:name="android.permission.READ_CONTACTS" />  activity_main.xml布局:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.android_25.MainActivity">    <ListView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/lv_lxr"      >    </ListView>  </LinearLayout>

activity_xs.xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_xs"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.android_25.XsActivity">  <TextView      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:id="@+id/tv_name"    />    <TextView      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:id="@+id/tv_telephone"    />  </LinearLayout>

MainActivity類:

private ListView lv_lxr;private Button b_name;private ContentResolver cr;private List<Map<String, Object>> datalistView;@Overrideprotected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  //獲得ListView  lv_lxr = (ListView) findViewById(R.id.lv_lxr);  //得到訪問者  cr = getContentResolver();  //定義一個接收聯系人姓名和電話號碼的集合  datalistView = new ArrayList<>();      Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");      Cursor cursor= cr.query(uri,null,null,null,null);      while(cursor.moveToNext()){        int id=cursor.getInt(cursor.getColumnIndex("_id"));        Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");        Cursor contactData= cr.query(uriData,null,null,null,null);        //用來裝姓名        String aa="";        //用來裝號碼        String bb="";        while(contactData.moveToNext()){          String type=contactData.getString(contactData.getColumnIndex("mimetype"));          //如果獲取的是vnd.android.cursor.item/phone_v2則是號碼          if(type.equals("vnd.android.cursor.item/phone_v2")){            bb=contactData.getString(contactData.getColumnIndex("data1"));            //如果獲取的是vnd.android.cursor.item/name則是姓名          }else if(type.equals("vnd.android.cursor.item/name")) {            aa=contactData.getString(contactData.getColumnIndex("data1"));          }        }        //將用戶名和號碼放入Map集合中        Map<String,Object> map=new HashMap<>();        map.put("images",aa);        map.put("titles",bb);        datalistView.add(map);      }  SimpleAdapter adapter=new SimpleAdapter(this, datalistView,R.layout.activity_xs,new String[]{"images","titles"},new int[]{R.id.tv_name,R.id.tv_telephone});  lv_lxr.setAdapter(adapter);}

第二種:通過Butten按鈕跳轉到系統的手機聯系人界面,單個獲取手機聯系人信息,展示在ListView中

activity_contacts.xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:id="@+id/activity_contacts"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"      tools:context="com.example.android_25.ContactsActivity">    <LinearLayout      android:layout_width="match_parent"      android:layout_height="wrap_content">        <Button          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="跳轉到聯系人頁面"          android:id="@+id/b_tzcontacts"          />    </LinearLayout>    <ListView      android:layout_width="wrap_content"      android:layout_height="wrap_content"        android:id="@+id/lv_contacts"      ></ListView></LinearLayout>

ContactsActivity類:

private Button b_tzcontacts;private String phoneName;private String phoneNumber;private List<Map<String,Object>> datalistView;private ListView lv_contacts;private SimpleAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_contacts);  //獲得跳轉到聯系人的id  b_tzcontacts =(Button) findViewById(R.id.b_tzcontacts);  //獲得ListView的ID  lv_contacts =(ListView) findViewById(R.id.lv_contacts);  //定義一個接受聯系人姓名和電話號碼的集合  datalistView = new ArrayList<>();  //獲取聯系人的點擊事件  b_tzcontacts.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View view) {      Intent intentPhone=new Intent(Intent.ACTION_PICK);      intentPhone.setData(ContactsContract.Contacts.CONTENT_URI);      startActivityForResult(intentPhone,0);    }  });  //R.layout.activity_xs就是上文的activity_xs布局問價  adapter = new SimpleAdapter(this, datalistView, R.layout.activity_xs,new String[]{"images","titles"},new int[]{R.id.tv_name,R.id.tv_telephone});  lv_contacts.setAdapter(adapter);}  //獲得返回的結果@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {  super.onActivityResult(requestCode, resultCode, data);  switch (requestCode){    case 0:      if(resultCode== Activity.RESULT_OK){        Uri uri=data.getData();        Cursor cursor=managedQuery(uri,null,null,null,null);        cursor.moveToFirst();        String contactid=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));        //得到ContentResolver        ContentResolver contentResolver=getContentResolver();        Cursor phone=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactid,null,null);        while (phone.moveToNext()){          //聯系人          phoneName =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));          //手機號碼          phoneNumber =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));          //格式化手機號          phoneNumber = phoneNumber.replace("-","");          phoneNumber = phoneNumber.replace("","");          //將用戶名和號碼放入Map集合中          Map<String,Object> map=new HashMap<>();          map.put("images",phoneName);          map.put("titles",phoneNumber);          datalistView.add(map);        }        //刷新適配器        adapter.notifyDataSetChanged();      }      break;  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵宝市| 寻乌县| 韶关市| 晋城| 承德市| 通州市| 视频| 黄平县| 鹤壁市| 左贡县| 民勤县| 石家庄市| 济阳县| 华容县| 资阳市| 永济市| 琼海市| 十堰市| 顺义区| 囊谦县| 田林县| 庄河市| 枞阳县| 新乡县| 嘉鱼县| 麦盖提县| 湛江市| 布拖县| 昌平区| 河西区| 涿州市| 辽阳市| 蚌埠市| 永州市| 合江县| 新野县| 米林县| 秦皇岛市| 徐州市| 咸宁市| 沂水县|