如何读取 android 中的所有联系人?
本示例演示了如何读取 android 中的所有联系人
步骤 1 − 在 Android Studio 中创建一个新项目,依次转到文件 ⇒ 新建项目,并填写所有必需的详细信息来创建一个新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:gravity = "center" android:layout_height = "match_parent"> <ListView android:id = "@+id/list" android:layout_width = "wrap_content" android:textSize = "30sp" android:layout_height = "wrap_content" android:text = "Click"/> </LinearLayout>
在以上代码中,我们选取了列表视图。
步骤 3 − 将以下代码添加到 src/MainActivity.java
<?xml version = "1.0" encoding = "utf-8"?> import android.annotation.TargetApi; import android.content.ContentResolver; import android.content.pm.PackageManager; import android.database.Cursor; import android.os.Build; import android.os.Bundle; import android.provider.ContactsContract; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { public static final int REQUEST_READ_CONTACTS = 79; ListView list; ArrayList mobileArray; @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { mobileArray = getAllContacts(); } else { requestPermission(); } list = findViewById(R.id.list); ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, mobileArray); list.setAdapter(adapter); } private void requestPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_CONTACTS)) { // show UI part if you want here to show some rationale !!! } else { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS); } if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_CONTACTS)) { } else { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_READ_CONTACTS: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { mobileArray = getAllContacts(); } else { // permission denied,Disable the // functionality that depends on this permission. } return; } } } private ArrayList getAllContacts() { ArrayList<String> nameList = new ArrayList<>(); ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if ((cur ! = null ? cur.getCount() : 0) > 0) { while (cur ! = null && cur.moveToNext()) { String id = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); nameList.add(name); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); while (pCur.moveToNext()) { String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); } pCur.close(); } } } if (cur ! = null) { cur.close(); } return nameList; } }
步骤 4 − 将以下代码添加到 AndroidManifest.xml
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.myapplication"> <uses-permission android:name = "android.permission.READ_CONTACTS" /> <application android:allowBackup = "true" android:theme = "@style/AppTheme.NoActionBar" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true"> <activity android:name = ".MainActivity" android:configChanges = "keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <action android:name = "android.intent.action.CREATE_SHORTCUT" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行你的应用程序。我假设你已将你的实际 Android 移动设备连接到了你的计算机。要从 android studio 运行应用,请打开你的一个项目的活动文件,然后单击工具栏中的运行 图标。选择你的移动设备作为选项,然后检查你的移动设备,它将显示你的默认屏幕 -
广告