首先,我先创建一个项目名为ContactsTest。
1.布局文件activity_main.xml
由于只是简单的实现读取联系人信息并展示出来,所以我的界面也十分简洁,只使用了一个ListView进行显示,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/contacts_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2.主活动MainActivity.java
package com.example.contactstest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
i