最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android獲取手機聯系人列表的方法

 更新時間:2020年10月23日 08:53:14   作者:程思陽  
這篇文章主要為大家詳細介紹了Android獲取手機聯系人列表的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android獲取手機聯系人列表的具體代碼,供大家參考,具體內容如下

下面直接貼代碼

1.先寫一個實體類,來放名字和號碼

public class PhoneDto {
    private String name;    //聯系人姓名
    private String telPhone;  //電話號碼
   
   
    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.寫我們獲取聯系人的工具類

public class PhoneUtil {
   
    // 號碼
    public final static String NUM = ContactsContract.CommonDataKinds.Phone.NUMBER;
    // 聯系人姓名
    public final static String NAME = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
   
    //上下文對象
    private Context context;
    //聯系人提供者的uri
    private Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
   
    public PhoneUtil(Context context){
      this.context = context;
    }
   
    //獲取所有聯系人
    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.接下來貼主頁面布局

<?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();
    }
   
    /**
     * 檢查權限
     */
    private void check() {
      //判斷是否有權限
      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增加點擊事件
      /*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.好了這樣的話就已經完成了。

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

相關文章

最新評論

察哈| 武乡县| 五常市| 邹城市| 靖边县| 垫江县| 潜江市| 周至县| 木兰县| 阿图什市| 隆安县| 密云县| 涿鹿县| 香港| 买车| 北辰区| 时尚| 闻喜县| 高平市| 安化县| 无棣县| 长丰县| 寿光市| 昆山市| 万宁市| 宝应县| 罗田县| 明溪县| 宣城市| 沭阳县| 阜新| 兰溪市| 桓仁| 时尚| 海林市| 肥东县| 湄潭县| 册亨县| 高雄县| 关岭| 临朐县|