Android獲取手機(jī)聯(lián)系人列表的方法
本文實(shí)例為大家分享了Android獲取手機(jī)聯(lián)系人列表的具體代碼,供大家參考,具體內(nèi)容如下
下面直接貼代碼
1.先寫(xiě)一個(gè)實(shí)體類(lèi),來(lái)放名字和號(hào)碼
public class PhoneDto { private String name; //聯(lián)系人姓名 private String telPhone; //電話號(hào)碼 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTelPhone() { return telPhone; } public void setTelPhone(String telPhone) { this.telPhone = telPhone; } public PhoneDto() { } public PhoneDto(String name, String telPhone) { this.name = name; this.telPhone = telPhone; } }
2.寫(xiě)我們獲取聯(lián)系人的工具類(lèi)
public class PhoneUtil { // 號(hào)碼 public final static String NUM = ContactsContract.CommonDataKinds.Phone.NUMBER; // 聯(lián)系人姓名 public final static String NAME = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME; //上下文對(duì)象 private Context context; //聯(lián)系人提供者的uri private Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; public PhoneUtil(Context context){ this.context = context; } //獲取所有聯(lián)系人 public List<PhoneDto> getPhone(){ List<PhoneDto> phoneDtos = new ArrayList<>(); ContentResolver cr = context.getContentResolver(); Cursor cursor = cr.query(phoneUri,new String[]{NUM,NAME},null,null,null); while (cursor.moveToNext()){ PhoneDto phoneDto = new PhoneDto(cursor.getString(cursor.getColumnIndex(NAME)),cursor.getString(cursor.getColumnIndex(NUM))); phoneDtos.add(phoneDto); } return phoneDtos; } }
3.接下來(lái)貼主頁(yè)面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.test.content.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_main_list"></ListView> </LinearLayout>
4.該貼主Activity代碼了
public class MainActivity extends AppCompatActivity { private List<PhoneDto> phoneDtos; private ListView lv_main_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); check(); } /** * 檢查權(quán)限 */ private void check() { //判斷是否有權(quán)限 if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},201); }else{ initViews(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode==201){ initViews(); }else{ return; } } private void initViews() { PhoneUtil phoneUtil = new PhoneUtil(this); phoneDtos = phoneUtil.getPhone(); lv_main_list = (ListView) findViewById(R.id.lv_main_list); MyAdapter myAdapter = new MyAdapter(); lv_main_list.setAdapter(myAdapter); //給listview增加點(diǎn)擊事件 /*lv_main_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //撥打電話 Intent intent = new Intent(); intent.setAction("android.intent.action.CALL"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("tel:"+phoneDtos.get(position).getTelPhone())); startActivity(intent); } });*/ } //自定義適配器 private class MyAdapter extends BaseAdapter { @Override public int getCount() { return phoneDtos.size(); } @Override public Object getItem(int position) { return phoneDtos.get(position); } @Override public long getItemId(int position) { return position; } @SuppressLint("NewApi") @Override public View getView(int position, View convertView, ViewGroup parent) { PhoneDto phoneDto = phoneDtos.get(position); LinearLayout linearLayout = new LinearLayout(MainActivity.this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.weight = 1; TextView tv_name = new TextView(MainActivity.this); tv_name.setId(View.generateViewId()); tv_name.setLayoutParams(layoutParams); tv_name.setText(phoneDto.getName()); TextView tv_num = new TextView(MainActivity.this); tv_num.setId(View.generateViewId()); tv_num.setLayoutParams(layoutParams); tv_num.setText(phoneDto.getTelPhone()); linearLayout.addView(tv_name); linearLayout.addView(tv_num); return linearLayout; } } }
5.好了這樣的話就已經(jīng)完成了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開(kāi)發(fā)之Service用法實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之Service用法,實(shí)例分析了Android中Service的功能及使用技巧,需要的朋友可以參考下2015-05-05okhttp3.4.1+retrofit2.1.0實(shí)現(xiàn)離線緩存的示例
本篇文章主要介紹了okhttp3.4.1+retrofit2.1.0實(shí)現(xiàn)離線緩存的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案
這篇文章主要介紹了 Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案的相關(guān)資料,需要的朋友可以參考下2016-12-12Android自定義圓形倒計(jì)時(shí)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義圓形倒計(jì)時(shí)進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題
最近幾天在學(xué)習(xí)使用phonegap進(jìn)行android應(yīng)用的開(kāi)發(fā),網(wǎng)上的資料比較亂,個(gè)人參考了很多資料,也試驗(yàn)了很多次,一直在摸索,總算小有心得,這此過(guò)程中也遇到了一些問(wèn)題,這里給大家分享下解決Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題的方法,這里簡(jiǎn)單的整理一下2015-05-05Android添加圖片到ListView或者RecyclerView顯示
這篇文章主要介紹了Android添加圖片到ListView或者RecyclerView顯示的相關(guān)資料,需要的朋友可以參考下2016-08-08Android實(shí)現(xiàn)藍(lán)牙(BlueTooth)設(shè)備檢測(cè)連接
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)藍(lán)牙(BlueTooth)設(shè)備檢測(cè)連接,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Flutter中g(shù)o_router路由管理的使用指南
go_router?是一個(gè)?Flutter?的第三方路由插件,相比?Flutter?自帶的路由,go_router?更加靈活,而且簡(jiǎn)單易用,下面小編就來(lái)和大家聊聊go_router的使用吧2023-08-08