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.はい、これで完成です。
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。