如何解决如何在Android App上列出所有移动联系人 ContactUtils.kt
我开始学习Android Studios的移动应用开发。我想创建一个简单的应用程序(用于测试),在其中以ListView显示所有手机联系人。
我有一个主要活动及其xml文件,在其中添加了ListView元素,我想在那里显示所有联系人。
这是主要活动类的代码:
package com.example.contactlist;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
这是主要活动XML的代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="66dp"
android:background="@color/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Contact Lists" />
<TextView
android:id="@+id/subtitulo_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Lists"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.087"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.119" />
<ListView
android:id="@+id/listado_contactos"
android:layout_width="346dp"
android:layout_height="644dp"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="21dp"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
android:layout_marginBottom="29dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/subtitulo_container"
tools:listitem="@android:layout/simple_list_item_multiple_choice" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是AndroidManifest文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactlist">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我想知道的是如何从手机中获取所有联系人数据并将其显示在ListView中?
解决方法
我已经开发了一个实用程序类来检索设备联系人。可以在GitHub上使用:
ContactUtils.kt
由于处理检索联系人中可能发生的所有情况都非常耗时,因此建议您获取此文件并将其添加到项目中。它是用kotlin
编写的,但是如果您使用的是java
,也可以获取如下的联系人列表:
List<ContactData> contacts = ContactUtilsKt.retrieveAllContacts(context);
// or to retrieve all contacts matching specific search pattern:
List<ContactData> contacts = ContactUtilsKt.retrieveAllContacts(context,"John");
,
这里是您可以获取联系人的功能
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,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));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",new String[]{id},null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG,"Name: " + name);
Log.i(TAG,"Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
,
您必须将列表推送或设置到适配器
public List<String> getNumber(ContentResolver cr)
{
List<String> phonebook = new ArrayList<>();
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null);
// use the cursor to access the contacts
while (phones.moveToNext())
{
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phonebook.add(phoneNumber);
}
return phonebook;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。